2012-07-16 34 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()