I'm having trouble retrieving the result of a query:
$a= "SELECT textfield FROM table WHERE key = $variable";
$b= mysql_query($a) or die("Query failed: " . mysql_error() . "$a<br>");
$c= mysql_fetch_array($b);
if ($c != $textfield){et cetra}
My if statement isn't working- when i printed the result of $c to debug, the result was "Array". there should only be one result, since field "key" is a key.
comparing query result to variable [SOLVED]
Moderator: General Moderators
comparing query result to variable [SOLVED]
Last edited by h123z on Wed Sep 19, 2007 6:38 am, edited 1 time in total.
Result of mysql_fetch_array is an array or a boolean false, as says the manual.
So $c variable in line:
returns an array or boolean false.
1. If select statement does not return any rows, the result of this line:
will be
2. If select statement returns some row, the result of this line:
will be e.g.:
So to get the value of textfield you need to get it from $c array, e.g.:
3. If your $textfield variable is a string you cannot (at least shouldn't) compare a boolean or an array to a string in line:
Please read manuals more carefully.
Michal
So $c variable in line:
Code: Select all
$c= mysql_fetch_array($b);1. If select statement does not return any rows, the result of this line:
Code: Select all
var_export($c);Code: Select all
falseCode: Select all
var_export($c);Code: Select all
array (
0 => 'TextValue',
'textfield' => 'TextValue',
)Code: Select all
$getTextfield = $c['textfield'];Code: Select all
if ($c != $textfield){et cetra}Michal
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
If you only need the returned field array referenced by the fieldname you may want to look at mysql_fetch_assoc. The only real difference is the amount of information returned but save where you can.