I tried to read out data from a specific date from a MySQL database.
What I wanted is for PHP to get all of the data from the database within a specific date that I entered, but it seems that the script just got only the first row of data and left out the rest.
Can you please help me with code to achieve this.
<?php
if(isset($_REQUEST['query']))
{
$searchQuery = $_POST['data-search'];
//connceting to db
$con = mysql_connect($_SERVER['SERVER_NAME'],'root','');
if(!$con)
{
die("Error connecting: ".mysql_error());
exit;
}
//selecting db
$dbSelect = mysql_select_db("directory",$con);
$query = "SELECT * FROM websites WHERE Date LIKE '$searchQuery'";
//executing query
$result = mysql_query($query,$con);
if(!$result)
{
die("Could not connect: ".mysql_error());
}
while($row = mysql_fetch_array($result))
{
$searchOutput = "<html><body><table border=1><tr><td>Website Title</td><td>Website URL</td><td>About Website Content</td></tr>
<tr><td>".$row['Title']."</td><td>".$row['URL']."</td><td>".$row['Content']."</td></tr></table>";
}
$searchSuccess = "<p>Your Search was successful</p</body></html>";
mysql_close($con);
echo $searchOutput;
echo $searchSuccess;
}
else{
echo "Invalid";
}
?>
In your script you replace
$searchOutputevery time it passes through the loop.Change:
to:
BTW you should consider refactoring your code so that all the templating/formatting stuff is separated from the database logic stuff. It will make it easier to read and maintain in the long run.