Search code examples
phpdatabaserecordset

Empty values in recordset


I'm trying to get data from my database - here's my code:

$rs = mysql_query("select * from u_gui where nam='".$nam."'",$db);
$total = mysql_num_rows($rs); // returns 1
if(!$total)
{
    echo "no records found.";
}
echo $rs["data"];

When I'm fetching the record works I'm getting 1 from mysql_num_rows() but when trying to echo the actual data i'm always getting blank results .. :(

any idea what's wrong?


Solution

  • The result returned by mysql_query is a ressource, in order to get the data from that ressource you need to call mysql_fetch_array or other mysql_ function that can parse that ressource.

    $rs = mysql_query("select * from u_gui where nam='" . mysql_real_escape_string($nam) . "'",$db);
    $total = mysql_num_rows($rs);
    
    if(!$total)
    {
        echo "no records found.";
    }
    
    $row = mysql_fetch_array($rs);
    echo $row["data"];
    

    On a side note, when you put non-sanitized data directly in a query (in your case $nam) make sure you apply mysql_real_escape_string to that variable before. Otherwise if your string contains character like ' it can produce sql error.