2014-11-24 122 views
0

所以我叫表query_logs - ID,查詢,date.Here是我的選擇查詢和日期代碼:從選擇兩列,三列顯示它

$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 />'; 
} 

,但它只是一遍又一遍地重複同樣的事情,直到$反= $計數。例如:

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

你能告訴我們完整的代碼片段嗎? – 2014-11-24 12:04:48

+0

好的,我編輯了我的評論。 – tsvmitev 2014-11-24 12:06:29

回答

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

非常感謝!我可以在8分鐘內選擇答案:) – tsvmitev 2014-11-24 12:09:12

+0

@tsvmitev,請選擇我的答案作爲解決方案。 – Pupil 2014-11-24 12:27:02

0

你那種似乎misinterprete什麼mysql_fetch_array( )呢。它只返回表格的一行值的數組,而不是你可以用計數器迭代的行數組(加上你不用循環中的計數器)。所以你必須再次爲你想讀的每一行調用它。使用while循環,您可以輕鬆遍歷整個表格,如mysql_fetch_array()將只返回false所有行都被讀取完畢。

while ($row = mysql_fetch_array($qlogs)) { 
    echo $row['date'].' - [ '.$row['query'].' ] <br />'; 
}