2012-07-16 38 views
1

當我運行下面的MySQL查詢錯誤選擇而生的MySQL和PHP

$result = mysql_query("SELECT * FROM credentials ORDER BY RAND() LIMIT 1"); 
$result =mysql_fetch_row($result); 
$username=$result["username"]; 
$password=$result["password"]; 
$gateway=$result["gateway"]; 

我獲得以下錯誤:

Notice: Undefined index: username in C:\xampp\htdocs\switch\switch\index.php on line 78

Notice: Undefined index: password in C:\xampp\htdocs\switch\switch\index.php on line 79

Notice: Undefined index: gateway in C:\xampp\htdocs\switch\switch\index.php on line 80

有人可以幫我嗎?

+1

分號是結束語句(第2行代碼)的好方法.. – 2012-07-16 12:02:05

+0

對不起,現在錯誤是 – 2012-07-16 12:04:35

+0

致命錯誤:調用成員函數fetchRow()on C:\ xampp \ htdocs \ switch \ switch \ index.php中的一個非對象,位於第77行 – 2012-07-16 12:04:41

回答

2

我不知道你的第78行是什麼,但你應該看看你的分號。

$result = $result->fetchRow() 

這裏是一個缺失。首先,你應該看看你自己並在寫這裏之前找到一個解決方案。

編輯:

$結果不是一個對象。使用

$result = mysql_fetch_row($result); 
+0

注意:未定義的索引:用戶名C:\ xampp \ htdocs \ switch \ switch \ index.php第78行 注意:未定義的索引:C:\ xampp \ htdocs \ switch \ switch \ index.php中的密碼79 注意:未定義的索引:第80行的C:\ xampp \ htdocs \ switch \ switch \ index.php中的網關 – 2012-07-16 12:11:48

+0

然後在需要密鑰時嘗試mysql_fetch_assoc。 – Stony 2012-07-16 12:14:56

2
$result = mysql_query("SELECT * FROM credentials ORDER BY RAND() LIMIT 1"); 
$result = $result->fetchRow(); 
$username=$result["username"]; 
$password=$result["password"]; 
$gateway=$result["gateway"]; 

你錯過了;後$結果= $ result-> fetchRow()

0

嘗試mysql_fetch_assoc代替和mysql_fetch_row

Fetch_row返回一個列舉的陣列例如$結果[0],而不是導致[ '密碼']

0

如果有與mysql_query返回一個布爾查詢錯誤..所以引入該一個檢查:

$result = mysql_query("SELECT * FROM credentials ORDER BY RAND() LIMIT 1"); 

if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 

$result = mysql_fetch_array($result); 
$username=$result["username"]; 
$password=$result["password"]; 
$gateway=$result["gateway"]; 

還要注意的是mysql_fetch_row返回一個數值陣列..返回關聯數組使用mysql_fetch_array()(WHI ch默認情況下返回)或者mysql_fetch_assoc()