mysqli_num_rows() expects parameter 1 to be mysqli

2020-02-07 10:17发布

I am not able to get the warning the sql query is working and mysqli_query() returns 1 and each time my database is also being updated . What is the problem?

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\qq\index.php on line 55

of the code which is

<?php
 try{
  $host="localhost";

 $user="deb";
 $password="12345";
 $database="1";
 $conn=mysqli_connect($host,$user,$password,$database);
 if (mysqli_connect_errno())
 {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 echo "connected succesfully";
   }catch(Exception $e){
 echo $e->getMessage();
 }
 $select=mysqli_select_db($conn,$database);
 if($select)
  {
  echo "yipee";
  }

echo "<table>
<thead>
<tr>
    <th>Id</th>
    <th>Value</th>
    <th>Threshold Value</th>
    <th>Chances of Fire</th>
</tr>
</thead>";
$val=$_GET["val"];

the error is in this code:

$tval=$_GET["tval"];
 if ($val<$tval) {
 $res="less ";
 }
else{$res="exreme ";}
$sql1="INSERT INTO sensor (val,tval,result) VALUES 
('".$val."','".$tval."','".$res."')"; 
 $que=mysqli_query($conn,$sql1);
 if($que)
  {
  if(mysqli_num_rows($que))
   {
    while ($data=mysqli_fetch_array($que))
     {
    echo "<tr>";
    echo "<td>".$data['id']."</td><td>".$data['val']."/td> 
       <td>".$data['tval']."</td><td>".$data['result']."</td>";
    echo"</tr>";
     }
   }

   else{
      echo "nooooooo";
        } 
      }     
      echo"</table>"
        ? >

even if i am removing mysqli_num_rows its showing error on

标签: php mysqli
2条回答
在下西门庆
2楼-- · 2020-02-07 10:30

According to another question here :

The INSERT command will return a boolean(true/false), So you must use select command to get the required result

查看更多
虎瘦雄心在
3楼-- · 2020-02-07 10:47

The error that you're getting is because the INSERT query failed, so mysqli_query() has returned false to indicate that the query has failed. A successful query would have returned a mysqli_result() object. Check the contents of $sql1 for anything like syntax errors.

You can use mysqli_error() after the insert query to get the error that's being returned by MySQL.

btw, you're wide open to SQL Injection attack! The value for $tval is coming straight from the user without any attempt to validate or sanitize it! You should use prepared statements.

查看更多
登录 后发表回答