tsvmitev
tsvmitev

Reputation: 69

Selecting two columns from and displaying it

So I have a table called query_logs with three columns - ID, query, date.Here's my code for selecting query and date:

$qlogs = mysql_query('select query, date from fcms.query_logs order by id desc');
$query_logs = mysql_fetch_array($qlogs);

I'm trying to display all the infomation from these columns:

for($counter = 1; $counter <= $count[0]; $counter++) {
echo $query_logs['date'].' - [ '.$query_logs['query'].' ] <br />'; 
}

But it just repeats the same thing over and over until $counter = $count.. For example:

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 ] 

The problem's here:

echo $query_logs['date'].' - [ '.$query_logs['query'].' ] <br />';

I've got no idea how to make it works ;/

Full code:

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

Upvotes: 0

Views: 63

Answers (2)

inVader
inVader

Reputation: 1534

You sort of seem to misinterprete what mysql_fetch_array() does. It returns you an array of values for one row of the table only, not an array of rows through which you can iterate with the counter (plus you don't use the counter in your loop anyway). So you have to call it again for every row you want to read. Using a while loop you can easily iterate through the whole table as mysql_fetch_array() will just return false once all rows have been read.

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

Upvotes: 0

Pupil
Pupil

Reputation: 23948

You need to loop through for mysql_fetch_array()

And $counter, does not make any sense here, so, please remove it.

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

Upvotes: 4

Related Questions