MySQL:未定义的索引,即使它们已定义

发布于 2024-12-12 04:43:23 字数 1376 浏览 0 评论 0原文

我有一些非常简单的代码:

$result = mysql_query("
SELECT  max(id) as id, ip, max(entry), COUNT(ip) AS count 
FROM table_name
GROUP BY ip
ORDER BY max(id) asc
");

$i = 0;

$num_rows = mysql_num_rows($result);

echo $num_rows;


while($row = mysql_fetch_row($result)) {
    $id = $row['id'];
    $entry = $row['entry'];
    $ip = $row['ip'];
    $count = $row['count'];
    $i++;
?>



<tr width="100%" align="center">
    <td><?php echo $i; ?></td>
    <td><?php echo $id; ?></td>
    <td><?php echo $entry; ?></td>
    <td><?php echo $ip; ?></td>
    <td><?php echo $count; ?></td>
    <td>
    <form style="display:inline;" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="hidden" value="<?php echo $ip; ?>" name="ip" />
        <input type="hidden" value="<?php echo $id; ?>" name="id" />
        <input type="submit" value="Ban IP" name="submit" />
    </form>
    </td>
</tr>

<?php
}

问题是,当我运行它时,我得到:

Notice: Undefined index: id
Notice: Undefined index: entry
Notice: Undefined index: ip
Notice: Undefined index: count

但据我所知,我已经在 SQL 语句中定义了索引,任何帮助将不胜感激。它使用列名 id、ip、entry 选择数据,并为 ip 计数创建索引“count”,那么为什么它说尚未定义呢?

I have some pretty simple code:

$result = mysql_query("
SELECT  max(id) as id, ip, max(entry), COUNT(ip) AS count 
FROM table_name
GROUP BY ip
ORDER BY max(id) asc
");

$i = 0;

$num_rows = mysql_num_rows($result);

echo $num_rows;


while($row = mysql_fetch_row($result)) {
    $id = $row['id'];
    $entry = $row['entry'];
    $ip = $row['ip'];
    $count = $row['count'];
    $i++;
?>



<tr width="100%" align="center">
    <td><?php echo $i; ?></td>
    <td><?php echo $id; ?></td>
    <td><?php echo $entry; ?></td>
    <td><?php echo $ip; ?></td>
    <td><?php echo $count; ?></td>
    <td>
    <form style="display:inline;" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="hidden" value="<?php echo $ip; ?>" name="ip" />
        <input type="hidden" value="<?php echo $id; ?>" name="id" />
        <input type="submit" value="Ban IP" name="submit" />
    </form>
    </td>
</tr>

<?php
}

The problem is that when I run it, I get:

Notice: Undefined index: id
Notice: Undefined index: entry
Notice: Undefined index: ip
Notice: Undefined index: count

But as far as I can see, I have defined the indexes in the SQL statement, any help would be appreciated. It selects the data using the column names id, ip, entry and creates the index "count" for the count of ip's, so why does it say that it hasn't been defined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

左耳近心 2024-12-19 04:43:23

要将结果行作为关联数组获取,您应该使用 mysql_fetch_assoc< /code>而不是 mysql_fetch_row

此外,尽管您声称已经定义了条目,但您尚未定义条目。改变这个:

SELECT max(id) as id, ip, max(entry), COUNT(ip) AS count 

到这个:

SELECT max(id) as id, ip, max(entry) AS entry, COUNT(ip) AS count 

To get the result row as an associative array you should use mysql_fetch_assoc instead of mysql_fetch_row.

Also you haven't defined entrydespite your claim that you have. Change this:

SELECT max(id) as id, ip, max(entry), COUNT(ip) AS count 

To this:

SELECT max(id) as id, ip, max(entry) AS entry, COUNT(ip) AS count 
梅倚清风 2024-12-19 04:43:23

您正在使用 mysql_fetch_row(),它将数据作为索引数组返回。您需要 mysql_fetch_assoc() 来代替。

You're using mysql_fetch_row(), which returns the data as an indexed array. You want mysql_fetch_assoc() instead.

明媚如初 2024-12-19 04:43:23

mysql_fetch_row 返回一个枚举数组。

您需要 mysql_fetch_array

或者,您可以按列索引获取值:

while($row = mysql_fetch_row($result)) {
    $id = $row[0];
    $ip = $row[1];
    $entry = $row[2];
    $count = $row[3];
    $i++;
}

mysql_fetch_row returns an enumerated array.

You want mysql_fetch_array.

Alternatively you could fetch values by column index:

while($row = mysql_fetch_row($result)) {
    $id = $row[0];
    $ip = $row[1];
    $entry = $row[2];
    $count = $row[3];
    $i++;
}
你如我软肋 2024-12-19 04:43:23

只需尝试 mysql_fetch_assoc 即可。

Just try mysql_fetch_assoc instead.

要走干脆点 2024-12-19 04:43:23

您需要mysql_fetch_assocmysql_fetch_row返回一个带有数字索引的普通数组。

You need mysql_fetch_assoc, mysql_fetch_row returns a plain array with numerical indexes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文