Click to See Complete Forum and Search --> : help with selecting one record (simple)


deep-wood
06-18-2006, 06:19 AM
im trying to get my head round php. I understand asp, so maybe this is where im falling....anyway


$str_id = 1;
$query="SELECT * FROM table1 where rec_id=$str_id";

$result=mysql_query($query);
$num=mysql_numrows($result);
if ($num > 0){
echo $result['rec_id'];
}else{
echo "nothing to show from sql<br />";
}


whats wrong with this? im just trying to select one record but im getting nothing.....:S

thanks for your time..

NogDog
06-18-2006, 09:17 AM
The return value of mysql_query() is a "resource id" and as such is not directly readable in any meaningful manner. (It is essentially a pointer to wherever the entire result set is being stored.) You need to use one of the mysql_fetch_*() functions to read a result row (by referencing that resource ID as your first argument). So....

if(mysql_num_rows($result) > 0 )
{
$row = mysql_fetch_assoc($result);
echo $row['rec-id'];
}
else
{
echo "nothing to show from sql";
}

See www.php.net/mysql_fetch_assoc for more info. At some point you'll find yourself wanting to use the while($row = mysql_fetch_assoc($result)) { pattern when you want to loop through the results of a query which may return multiple rows.