Fuxi
Fuxi

Reputation: 7599

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?

Upvotes: 0

Views: 315

Answers (3)

Micah Carrick
Micah Carrick

Reputation: 10197

This might help you better understand:

$rs = mysql_query("select * from u_gui where nam='".$nam."'",$db);
$total = mysql_num_rows($rs); // returns 1

if($total > 0)
{
    while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) 
    {
        foreach ($row as $key => $value)
        {   
            echo "$key = $value<br/>";
        }
    }
}

Upvotes: 0

HoLyVieR
HoLyVieR

Reputation: 11134

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.

Upvotes: 1

bensiu
bensiu

Reputation: 25584

if($total == 0)
{
    echo "no records found.";
}
print_r $rs;

and make sure you are connected...

Upvotes: 0

Related Questions