Check value in array returned by mysql_fetch_row in PHP
Hello i am new at php.
$res=mysql_query('SELECT `order` FROM `competitions`');
$row=mysql_fetch_row($res);
if($row[order] == "1")
{
$res=mysql_query('SELECT `name`, `persp`, `teamp` FROM `competitions_rank` WHERE `comp_id` = '.$_PARAMS[0].' ORDER BY `persp` DESC');
}
else
{
$res=mysql_query('SELECT `name`, `persp`, `teamp` FROM `competitions_rank` WHERE `comp_id` = '.$_PARAMS[0].' ORDER BY `teamp` DE开发者_运维技巧SC');
}
In the database the possible values of the field is null, 1 or 2 .The code always enter in else statement. What i do wrong ? How i should check value in array returned by mysql_fetch_row ?
Well, first of all, you shouldn't use mysql_*
functions. They're (unofficially) deprecated and not recommended for further use. Second, use $row["order"]
and not $row[order]
as there is no such thing as order
in PHP.
Also, mysql_fetch_row
returns the row as a numeric array, and not associative. if you insist on using mysql_
, use mysql_fetch_assoc()
.
In addition to that, that entire statement should be inside a while loop because there could be more then one line that you are missing. If you want to make sure you only have 1 line, add LIMIT 1
to the end of your SQL query.
this mysql_fetch_row
function,
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
so in your example you need to check it like :
if ($row[0] == "1")
I think problem is in mysql_fetch_row()
...
mysql_fetch_row
— Get a result row as an enumerated array.
USE
mysql_fetch_array()
then
use $row['order']
I'm not quite sure what you're asking but could you update your sql query to
$res=mysql_query('SELECT `order` FROM `competitions ORDER BY order`');
Failing that you need to change
if($row[ 'order' ] == "1")
精彩评论