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

时间:2021-01-03 02:11:10

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?

但据我所知,我已经在SQL语句中定义了索引,任何帮助都将受到赞赏。它使用列名id,ip,entry选择数据,并为ip的计数创建索引“count”,那么为什么它说它还没有定义?

5 个解决方案

#1


3  

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

要将结果行作为关联数组,您应该使用mysql_fetch_assoc而不是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 

#2


0  

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

您正在使用mysql_fetch_row(),它将数据作为索引数组返回。你想要mysql_fetch_assoc()。

#3


0  

mysql_fetch_row returns an enumerated array.

mysql_fetch_row返回一个枚举数组。

You want mysql_fetch_array.

你想要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++;
}

#4


0  

Just try mysql_fetch_assoc instead.

只需尝试使用mysql_fetch_assoc。

#5


0  

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

你需要mysql_fetch_assoc,mysql_fetch_row返回一个带有数字索引的普通数组。

#1


3  

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

要将结果行作为关联数组,您应该使用mysql_fetch_assoc而不是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 

#2


0  

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

您正在使用mysql_fetch_row(),它将数据作为索引数组返回。你想要mysql_fetch_assoc()。

#3


0  

mysql_fetch_row returns an enumerated array.

mysql_fetch_row返回一个枚举数组。

You want mysql_fetch_array.

你想要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++;
}

#4


0  

Just try mysql_fetch_assoc instead.

只需尝试使用mysql_fetch_assoc。

#5


0  

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

你需要mysql_fetch_assoc,mysql_fetch_row返回一个带有数字索引的普通数组。