Jump to content

mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given


eljaydee

Recommended Posts

I have searched the internet about this and found hundreds have asked the question and not once was it answered in a meaningful way--or maybe I'm just dense.

Would somebody please tell me what is the problem here:

Simple, simple form:

<html>
<body>

<form action="send_post.php" method="post">  
   <p>A: <input type="text" name="A">  
   <p>B: <input type="text" name="B">                                       
   <p><input type="submit">    
</form>     

</body>
</html>

===========

Simple, simple PHP script: 

      <?php
      
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'xyz';
         $dbname = 'ABC';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
   
         if($conn)
         echo 'Connected successfully. <br> Here are the results of your query:<br><br>';
         
         $A=$_POST["A"];
         $B=$_POST["B"];
         
         $sql = 'SELECT * FROM numbers WHERE A = $A AND B = $B';
         $result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_array($result)) {
               echo $row["0"] . ", &nbsp;";
            }
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
         
          ?>     
      
      Which is 'parameter 1'? and which is the 'bool given'?

But most importantly, how should it be written so that it returns the desired results? I have checked the query from the CMD line, it returns multiple entries.

Really, I have reached FRUSTRATION OVERLOAD!

Edited by eljaydee
Link to comment
Share on other sites

Your query is failing. You are using single quotes so the variables are not being interpolated. You should be using Prepared Statements anyways. NEVER EVER put variables in the query and don't create variables for nothing. There is no need to manually close the connection. Php will do it automatically.

I would also recommend you use PDO. Here is a tutorial to get you going.

https://phpdelusions.net/pdo

Edited by benanamen
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.