2011-10-25 33 views
2

我有一些非常簡單的代碼:MySQL的:未定義的指標,即使它們已經被定義

$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,進入選擇數據和IP公司的計數創建索引「計數」,那麼爲什麼它說,它沒有被定義?

+0

對不起,如果描述似乎稀疏,我只是不知道如何描述它 – AviateX14

回答

3

要得到的結果行作爲你應該使用mysql_fetch_assoc代替mysql_fetch_row關聯數組。

還有你還沒有定義entry儘管你聲稱你有。更改此:

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 
+0

立即排序,謝謝,將在11分鐘內接受。 – AviateX14

0

您使用mysql_fetch_row(),返回數據作爲索引數組。您需要改爲mysql_fetch_assoc()

0

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++; 
} 
0

試試看mysql_fetch_assoc來代替。

0

你需要mysql_fetch_assocmysql_fetch_row返回與數字索引的普通數組。

相關問題