Reputation: 1734
Im using a PHP search function on my website and it is currently only displaying one result from my SQL database - i would like it to display all results included in the site_keywords!
Here is the PHP code i'm currently using on my page
<?php
mysql_connect("danieljosephdesignsc.ipagemysql.com", "searchdata", "danieljoseph");
mysql_select_db("my_db");
if(isset($_GET['search'])) {
$search_value = $_GET['value'];
$query = "select * from sites where site_keywords like '%$search_value%'";
$run = mysql_query($query);
while($row=mysql_fetch_array($run)){
$title = $row['site_title'];
$link = $row['site_link'];
$desc = $row['site_desc'];
}
}
?>
Here is what is included in my body:
<?php echo "<div><a href='$link'><h2>$title</h2></a><p>$desc</p><a href='$link'>$link</a></div>";?>
Please let me know if you require further info. Any assistance on this would be greatly appreciated.
Thanks
Upvotes: 1
Views: 570
Reputation: 2962
You problem is that you aren't echoing the results within your loop.
you need something like this:
while($row=mysql_fetch_assoc($run)){
$title = $row['site_title'];
$link = $row['site_link'];
$desc = $row['site_desc'];
echo "<div><a href='$link'><h2>$title</h2></a><p>$desc</p><a href='$link'>$link</a></div>";
}
Important Note:
You are using deprecated code. This is true of any function that begins with mysql_
. This means that your code will no longer work in newer versions of php, and is likely to be not secure if you call any of your variables in your queries. You really need to look up using prepared statements using either mysqli or PDO.
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_connect() PDO::__construct()
Here's how to use mysqli prepared statements or PDO prepared statements
Upvotes: 1
Reputation: 1058
$rows =array();
while($row=mysql_fetch_array($run)){
$rows[] =$row;
}
foreach($rows as $row){
$title = $row['site_title'];
$link = $row['site_link'];
$desc = $row['site_desc'];
echo "<div><a href='$link'><h2>$title</h2></a><p>$desc</p><a href='$link'>$link</a></div>";
}
Upvotes: 0
Reputation: 12090
The problem is in this block of code:
while($row=mysql_fetch_array($run)){
$title = $row['site_title'];
$link = $row['site_link'];
$desc = $row['site_desc'];
}
Even though you're getting multiple results, each result overwrites the values of the previous one. So in the end you only have a single row. Try saving the results to an array instead and iterating over it in the body.
Upvotes: 0