所以我有一个名为query_logs的表,其中包含三列--ID,query,date。这是我选择查询和日期的代码:
$qlogs = mysql_query('select query, date from fcms.query_logs order by id desc');
$query_logs = mysql_fetch_array($qlogs);
我正在尝试显示这些列中的所有信息:
for($counter = 1; $counter <= $count[0]; $counter++) {
echo $query_logs['date'].' - [ '.$query_logs['query'].' ] <br />';
}
但它只是一遍又一遍地重复同样的事情,直到$ counter = $ count ..例如:
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
2014-11-24 12:55:59 - [ select * from world.version ]
问题出在这里:
echo $query_logs['date'].' - [ '.$query_logs['query'].' ] <br />';
我不知道如何使其有效; /
完整代码:
<link rel="stylesheet" href="style.css" type="text/css" />
<form name="form" action="" method="get">
<label for="query">Query</label><textarea name="query" cols="40" rows="5"></textarea><br />
<label for="pass">Password</label><input type="password" name="pass" id="pass" /><br />
<input type="submit" value="Enter"/>
</form>
<?php
include('query_configuration_xf1hfa2xaz.php');
$query = $_GET['query'];
$password = $_GET['pass'];
$date = date('Y-m-d H:i:s');
$qlogs = mysql_query('select query, date from fcms.query_logs order by id desc');
$query_logs = mysql_fetch_array($qlogs);
$countq = mysql_query('select count(id) from fcms.query_logs');
$count = mysql_fetch_row($countq);
if($password != '') {
if($password == $security) {
$runquery = mysql_query($query);
$result = mysql_fetch_array($runquery);
if($runquery == true)
{
echo '<br /> The query was successfully executed! <br />';
mysql_query('insert into fcms.query_logs (query, date) values ("'.$query.'", "'.$date.'") ');
}
else
{
echo '<br />There\'s some error with your query! Check it again..<br />'.mysql_errno()." : "
.mysql_error();
}
}
else
{
echo 'The password is wrong!';
}
echo $result[0].'<br />';
}
for($counter = 1; $counter <= $count[0]; $counter++) {
echo $query_logs['date'].' - [ '.$query_logs['query'].' ] <br />';
}
?>
答案 0 :(得分:4)
您需要遍历mysql_fetch_array()
$counter
,这里没有任何意义,所以,请将其删除。
$qlogs = mysql_query('select query, date from fcms.query_logs order by id desc');
while ($query_logs = mysql_fetch_array($qlogs)) {
echo $query_logs['date'].' - [ '.$query_logs['query'].' ] <br />';
}
答案 1 :(得分:0)
你似乎错误地解释了 mysql_fetch_array()的作用。它只为表的一行返回一个值数组,而不是一个行数组,您可以通过这些行与计数器进行迭代(另外,您不要在循环中使用计数器)。因此,您必须再次为要阅读的每一行调用它。使用while循环,您可以轻松地遍历整个表,因为 mysql_fetch_array()只有在读取了所有行后才会返回 false 。
while ($row = mysql_fetch_array($qlogs)) {
echo $row['date'].' - [ '.$row['query'].' ] <br />';
}