简体   繁体   中英

SQL Query not Executing Properly

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

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.';");

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."'"
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM