1

Heres my code

<?php
session_start();

include('config.php');
if(isset($_GET['search_word']))
{
    // echo $_GET['search_word'] . '<br />'; // debugging
    $search_word = $_GET['search_word'];
    $search_word = mysql_escape_string($search_word);
    $search_word_fix = str_replace(" ","%",$search_word);

    $query = "SELECT * FROM article WHERE article_title LIKE '%" . $search_word . "%' AND article_live = '1' ORDER BY article_id DESC";
    // echo $query . '<br />'; // debugging

    $sql = mysql_query($query);
    $count = mysql_num_rows($sql);
    // echo $count . '<br />'; // debugging
    // echo mysql_num_rows($sql) . '<br />'; // debugging
    if($count > 0)
    {
        while($row=mysql_fetch_array($sql))
        {
            $msg=$row['article_title'];
            $bold_word='<b>'.$search_word.'</b>';
            $final_msg = str_ireplace($search_word, $bold_word, $msg);

            echo $final_msg; 
        }
    }
    else
    {
        echo "No Results";
    }
}?>

Can anyone see an issue with it? I cant pick out what is not working with this script and ive been staring at it for a while. It never makes it to the WHILE loop only the "No Results" and the count returns blank when i uncomment my debugging.

4

1 回答 1

3

计数返回空白意味着您的查询由于某种原因失败。

  • 您是否正确连接到数据库?尝试在查询后立即使用 mysql_error() :

    $error_msg = mysql_error();
    
  • 使用 mysql_real_escape_string() 而不是 mysql_escape_string() - 它尊重字符集,因此您不会遇到 UTF8 问题

如果您公开使用它,您可能想了解如何使用绑定来消除通过 PDO 之类的库进行 SQL 注入的可能性。

这是一个很好的 PDO 教程/介绍,解释了它为什么很重要!

于 2010-07-25T16:13:27.040 回答