空值

2011-03-19 58 views
0

我想從我的數據庫獲取數據 - 在這裏是我的代碼:空值

$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"]; 

當我獲取記錄的作品,我從mysql_num_rows獲得1(),但嘗試時呼應的實際數據我一直都想與空白結果.. :(

任何想法有什麼不對?

+0

請你在這裏展示表'u_gui'結構。 – anubhava 2011-03-19 17:47:21

回答

1

mysql_query返回的結果是一個資源,爲了從該資源中獲取數據,您需要調用mysql_fetch_array或其他可以解析該資源的mysql_函數。

$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"]; 

在一個側面說明,當你把非殺毒的數據直接在查詢(在你的情況$nam)確保你之前申請mysql_real_escape_string該變量。否則,如果你的字符串包含像'這樣的字符,它可能會產生sql錯誤。

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

,並確保您連接...

0

這可以幫助你更好的理解:

$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/>"; 
     } 
    } 
}