Check if value exists In MySQL row
Solution 1
Your query should be:
SELECT `header` FROM `data` WHERE `var` = '$foo'
This will return all the headers with a var
value of $foo
.
$db = mysqli_connect('localhost', 'username', 'password', 'database');
if($query = mysqli_query($db, "SELECT `header` FROM `data` WHERE `var` = '$foo'")){
while($row = mysqli_fetch_assoc($query)){
echo $row['header'];
}
mysqli_free_result($query);
}
Solution 2
It's not difficult at all, If I understand correctly then this should help you.
// Query Variable / Contains you database query information
$results = $query;
// Loop through like so if the results are returned as an array
foreach($results as $result)
{
if(!$result['var'])
echo $result['header'];
}
// Loop through like so if the results are returned as an object
foreach($results as $result)
{
if(!$result->var)
echo $result->header;
}
Solution 3
first connect to the db
$query = mysql_query("SELECT var, header FROM data WHERE id='1'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
if($foo == $row['var']){
echo $row['header'];
}
}
EDIT: changed equality statement based on your edit
Solution 4
are you asking if $foo
matches any of the fields in data
, or if $foo=some_field
? Here for if you want $foo==var
.
$foo='somevalue';
$query="SELECT id, var, header FROM `data` WHERE var='$foo'";
$result=mysqli_query($query);
if($result->num_rows==0)
$loc= 'http://google.com';//default value for when there is no row that matches $foo
}else{
$row=$result->fetch_assoc(); //more than one row is useless since the first header('Location: x') command sends the browser to a new page and away from your script.
$loc=$row['header'];
}
header ("Location: $loc);
exit;
ETA: since you've edited your question, it appears that you want to echo the header column if your search value matches your var column. The above won't work for that.
Trufa
Existing code exerts a powerful influence. Its very presence argues that it is both correct and necessary.
Updated on May 15, 2020Comments
-
Trufa about 4 years
I have a php variable:
$foo
My MySQL table called
data
has the following structure:id var header 1 zj3 http://google.com
I would like to check if
$foo
is all ready invar
row.If it is I would like to echo
header
("http://google.com")How would you approach this?
Thanks in advance, please ask if any clarification is needed!
-
Trufa over 13 yearsThis makes sense, I'll try it out, thanks for the quick response!
-
Trufa over 13 yearsYes, this is correct, I want to echo the header column if your search value matches your var column.
-
Trufa over 13 yearsSo if I compare it to empty, I will know if the value exists? what will it return if it doesn't find any value?
-
gen_Eric over 13 yearsIf it doesn't find anything, it'll return nothing (
$query
will beFALSE
). -
gen_Eric over 13 years@Trufa: What do you mean by 'So if I compare it to empty, I will know if the value exists'?
-
Trufa over 13 yearsI'm getting Parse error: syntax error, unexpected '{' in /home/xxx/public_html/xxx.com/index.php on line 11
-
Trufa over 13 yearsIt says: "No database selected" why could this be?
-
Andrew over 13 yearsAdd another
)
before the{
in the first line. -
gen_Eric over 13 yearsSeems I missed a
)
. Should be ok now. -
Trufa over 13 years
Warning: mysqli_query() expects at least 2 parameters
, sorry to bother!! -
gen_Eric over 13 years@Trufa: You need to make sure you connect to the DB using
mysql_connect
first. -
Trufa over 13 years@Rocket I am really sorrry I have to bother you again!!
Parse error: syntax error, unexpected T_IF
-
gen_Eric over 13 yearsOops, missed a
;
after$db=mysqli_connect
. -
Trufa over 13 yearsThank you, thank you!!! (+1 :) ) It's working right now, but I'm getting a warning:
Warning: mysqli_result::fetch_assoc() [mysqli-result.fetch-assoc]: Couldn't fetch mysqli_result in
ideas, sorry for all the inconvenience!! -
Trufa over 13 years@Rocket any ideas on the warning? Again, sorry to bother!
-
gen_Eric over 13 yearsChange
$query->fetch_assoc()
tomysqli_fetch_assoc($result)
. See updated answer. -
Trufa over 13 years@Rocket, I got a different one now:
Warning: mysqli_fetch_assoc() [function.mysqli-fetch-assoc]: Couldn't fetch mysqli_result in
I'm sorry I have to ask you everything, I have no idea what this errors mean, Thank you!! -
gen_Eric over 13 years
mysqli_free_result
should be outside the while loop. -
Trufa over 13 years@Rocket - Would hug you right now ;) All kidding aside, thanks for all your help and sorry for my ignorance!! Thanks again for the effort!
-
gen_Eric over 13 years@Trufa: I'll take that hug. It's ok, really, I'm here to help, and you're here to learn.
-
crh225 about 11 yearsShould this be if ( == )
-
gen_Eric about 11 years@crh225: Actually, no. I'm not comparing
$query
tomysqli_query
. I'm setting$query
tomysqli_query
, and checking the result of the assignment (=
). Ifmysqli_query
returnsFALSE
theif()
won't run, otherwise it would. -
Vulkan about 9 yearsOK, I don't know why you people think this is correct but I tell you not only that is false but that is a dangerous answer...
-
gen_Eric about 9 years@user3737190: The only dangerous thing is the lack of escaping. This answer is 4 years old, so it's very out-of-date. The current answer would be to use prepared statements.