1

当我运行以下 MySQL 查询时

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

我收到以下错误:

注意:未定义索引:第 78 行 C:\xampp\htdocs\switch\switch\index.php 中的用户名

注意:未定义索引:第 79 行 C:\xampp\htdocs\switch\switch\index.php 中的密码

注意:未定义索引:第 80 行 C:\xampp\htdocs\switch\switch\index.php 中的网关

有人可以帮帮我吗?

4

4 回答 4

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

You missed a ; after $result = $result->fetchRow()

于 2012-07-16T12:06:01.163 回答
2

我不知道你的第 78 行是什么。但你应该看看你的分号。

$result = $result->fetchRow()

这里缺少一个。首先,您应该自己查看并找到解决方案,然后再写到这里。

编辑:

$result 不是一个对象。利用

$result = mysql_fetch_row($result);
于 2012-07-16T12:05:27.340 回答
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()(默认情况下返回两者)或mysql_fetch_assoc()

于 2012-07-16T13:41:31.393 回答
0

尝试 mysql_fetch_assoc 而不是 mysql_fetch_row

Fetch_row 返回一个枚举数组,例如 $result[0] 而不是 result['password']

于 2012-07-16T13:37:48.780 回答