1

I made a table that holds post and likes and I'm trying to output the of everything in the table but I get the

error: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given on line 18.

$sql3 = "SELECT * FROM post;";
 $result = mysqli_query($conn,$sql3);
 $datas = array();
 if (mysqli_num_rows($result)> 0){
     while($row = mysqli_fetch_assoc($result)){
         $datas[] = $row;
     }
 }
     foreach ($datas as $data){
         echo $data;
     }

I expect the all the information to be outputted, but the actual output is

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given on line 18.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • either your query not qorking or connection not established. – devpro Aug 02 '19 at 16:21
  • Your query is failing, or you're attempted on an un-opened connection. Make sure [errors are displaying](https://stackoverflow.com/a/21429652/1843510) and `var_dump()` `$result` and `$conn`. – zbee Aug 02 '19 at 16:22
  • The accepted answer in https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments-mysqli-fetch-as says nothing about mysqli error methods and depends on the dev having access to configure errors on the host, which they may not have. – OneLiner Aug 07 '19 at 02:28
  • @oneliner using the MySQLi error methods as you suggested in the answer is a bad practice and is not recommended. The accepted answer shows how to properly enable MySQLi errors. – Dharman Aug 07 '19 at 08:54
  • @dharman To say that is bad practice is ridiculous. It is a part of mysqli itself. Also the accepted answer does NOT show how to get mysqli errors without reconfiguring php itself, which a dev may or may not have permission to do. You may NOT want php to throw an exception on query error. Your comment of "it is bad practice " is extremely subjective. The features are there to use. The answer in that link shows only one way to get there, which may not fit all cases. – OneLiner Aug 07 '19 at 13:17

1 Answers1

0

This means that your query is returning false instead of a mysqli_result object. The problem appears to be from the semicolon at the end of your statement. I believe that mysqli will see that as a multistatement. From https://www.php.net/manual/en/mysqli.quickstart.multiple-statement.php:

Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.

mysqli has a method to get the last error it encountered:

https://www.php.net/manual/en/mysqli.error.php

Example:

if(!$result){
  printf("Error message: %s\n", mysqli_error($conn));
}
OneLiner
  • 571
  • 2
  • 6
  • There are examples in the second link I posted. Also you should var_dump $result to examine it. Answer updated with mysqli_error code. – OneLiner Aug 07 '19 at 02:17
  • You do not have to reconfigure PHP to enable MySQLi exceptions with this line of code: `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. I am not saying that you should never use this methods, but **the way you suggested** is not recommended. Displaying errors to end users either through `print`, `echo` or `die` can leave your website vulnerable and is not user-friendly. It also adds an unnecessary level of complexity to your code. It is perfectly fine if you do it only as a temporary debugging measure, but do not push this code into production. – Dharman Aug 07 '19 at 18:23
  • Also see this very useful post: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Aug 07 '19 at 18:26
  • @dharman That requires PHP to be configured to display errors. It propagates errors up to the php error reporting. And using mysqli error methods does not require that you print the error message (although commonly used for debugging). You can parse the error message and apply logic. This may or may not be a good idea depending on the application and needs. And "the way you suggested is not recommended " again is based on opinions. It IS recommended by some, regardless of the latest SO post or blog you read before this one. – OneLiner Aug 08 '19 at 02:52