dongyi9783 2015-07-25 14:13
浏览 100
已采纳

PHP搜索仅显示SQL的一个结果

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

  • 写回答

3条回答 默认 最新

  • dongluo8439 2015-07-25 14:27
    关注

    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.

    From the official site

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP