Count always returns 1…but does it exist?

后端 未结 1 1317
名媛妹妹
名媛妹妹 2021-01-28 04:00

I\'m trying to check if a file name already exists in the \"reviews\" column within a particular category, before it\'s created. If it already exists, I want to add today\'s dat

1条回答
  •  北海茫月
    2021-01-28 04:09

    When you use COUNT() in your query you will always get one row returned even if only to tell you the count is zero. You need to check the value of COUNT(reviews) to get that value:

    $result = mysqli_query($sqli, "SELECT count(reviews) AS `count` FROM myTable WHERE `category`='$list' AND `reviews`='$php_file_name'");
    $check = mysqli_fetch_assoc($result);
    
    if($check['count'] >= 1){
    

    You'll notice that I gave count(reviews) an alias as it makes accessing that value easier in the PHP code.

    0 讨论(0)
提交回复
热议问题