-1

There seems to be an error with my SQL query. I keep getting this warning:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in

which I'm pretty sure points to a syntax error, I found an SQL checker online and it said there's an error as well. I've been looking over this for days and I just can't seem to find what's wrong.

Here's the code:

    function countReplies($cid, $scid, $tid, $mysqli){
        $select = mysqli_query($mysqli, "SELECT category_id, subcatgory_id, topic_id FROM replies WHERE ".$cid." = category_id AND ".$scid." = subcategory_id AND ".$tid." = topic_id");

        return mysqli_num_rows($select);
   }

EDIT:

After using

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

This is the error I'm getting now:

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''12' at line 6 in C:\xampp\htdocs(A)Book 2.0\Bootstrap\content_function.php:116 Stack trace: #0 C:\xampp\htdocs(A)Book 2.0\Bootstrap\content_function.php(116): mysqli_query(Object(mysqli), 'SELECT category...') #1 C:\xampp\htdocs(A)Book 2.0\Bootstrap\readtopic.php(55): countReplies('1', '2', '12', Object(mysqli)) #2 {main} thrown in C:\xampp\htdocs(A)Book 2.0\Bootstrap\content_function.php on line 116

CaptainAmerica16
  • 236
  • 2
  • 11
  • 1
    Possible duplicate of [How to get MySQLi error information in different environments](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments) – rickdenhaan Dec 08 '18 at 21:45
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/184948/discussion-on-question-by-captainamerica16-sql-query-not-executing-properly). – Samuel Liew Dec 09 '18 at 01:26

2 Answers2

1

i think you should be doing the ids / actual values you put inside of the query after the '=' sign. Like:

"SELECT category_id, subcatgory_id, topic_id FROM replies WHERE category_id = '.$cid.' 
 AND subcategory_id = '.$scid.' AND topic_id = '.$tid.';");
saFari
  • 23
  • 6
0

You have errors in your query :

  • field names must go to the left side of the « = » operand, values go to the right side

  • field values, when of type string (which, I assume, is the case in your query), must be enclosed in single quotes (or better yet, use bind parameters / this is the standard to pass variable values to a sql query).

Code :

$select = mysqli_query($mysqli, 
    "SELECT category_id, subcatgory_id, topic_id 
    FROM replies 
    WHERE 
        category_id = '".$cid."'
        AND subcategory_id = '".$scid."'
        AND topic_id = '".$tid."'"
);
GMB
  • 216,147
  • 25
  • 84
  • 135
  • *"field values must be enclosed in single quotes"* - Only if they're strings. Integers are handled differently. *"or better yet, use bind parameters"* that is a for sure. You forgot the `$` for this `select` btw. – Funk Forty Niner Dec 08 '18 at 22:07
  • Thanks @FunkFortyNiner, fixed the answer – GMB Dec 08 '18 at 22:18