为什么这个sql语句一直说它是布尔值而不是参数? (PHP / MySQL的)

时间:2010-04-17 18:40:01

标签: php mysql

在此声明中,我试图查看数据库中是否存在具有完全相同的标题,价格,城市,州,详细信息的最新发布。如果有,那么它会告诉用户确切的帖子已经发出;如果没有,则将发布内容插入dbc。 (这是一种类型的检查,以便用户不会意外发布两次。这可能不是最好的检查,但这个语句错误让我讨厌,所以我希望它能够工作:))

为什么这个sql不起作用?我认为这不是让title=$title而不是$title中的价值......

> ERROR: mysqli_num_rows() expects
> parameter 1 to be mysqli_result,
> boolean given in postad.php on line
> 365

    //there is a form that users fill out that has title, price, city, etc
    <form>
    blah blah
    </form>

         //if users click submit, then does all the checks and if all okay, insert to dbc
        if (isset($_POST['submit'])) 
        {

        // Grab the pposting data from the POST and gets rid of any funny stuff
        $title = mysqli_real_escape_string($dbc, trim($_POST['title']));
        $price = mysqli_real_escape_string($dbc, trim($_POST['price']));
        $city = mysqli_real_escape_string($dbc, trim($_POST['city']));
        $state = mysqli_real_escape_string($dbc, trim($_POST['state']));
        $detail = mysqli_real_escape_string($dbc, trim($_POST['detail']));

         if (!is_numeric($price) && !empty($price))
         {
            echo "<p class='error'>The price can only be numbers. 
                No special characters, etc</p>";
        }

        //Error problem...won't let me set title=$title, detail=$detail, etc.            
         //this statement after all the checks so that none of the variables are empty
        $query="Select * FROM posting 
    WHERE user_id={$_SESSION['user_id']}
    AND title=$title
    AND price=$price
    AND city=$city
    AND state=$state
    AND detail=$detail";
    $data = mysqli_query($dbc, $query);
    if(mysqli_num_rows($data)==1) 
    {
        echo "You already posted this ad. Most likely caused by refreshing too many times.";
    }

}

4 个答案:

答案 0 :(得分:2)

哇我猜我应该回到基本的Mysql规则......

$query="Select * FROM posting 
WHERE user_id='{$_SESSION['user_id']}'
AND title='$title'
AND price='$price'
AND city='$city'
AND state='$state'
AND detail='$detail'
";

那些''是造成错误的原因

答案 1 :(得分:1)

mysqli_query($dbc, $query)正在返回FALSE,您要将其分配给$data并传递给mysqli_num_rows

请参阅this page,了解可能导致其返回false的原因。

答案 2 :(得分:0)

尝试像这样的num rows条件

if(mysqli_num_rows($data)>0) {
}

答案 3 :(得分:0)

手动(mysqli_query()):

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query()  will return a result object. For other successful queries mysqli_query()  will return TRUE. 

所以你在查询中有错误。