mysqli_num_rows() expects parameter 1 to be mysqli_result, object

前端 未结 3 1805
天命终不由人
天命终不由人 2020-12-11 22:10

This is my first experience with mysqli. It seems to be looking for the name of a result set in between the parentheses in mysqli_num_rows(). Yet when I tried $stmt, $conn,

相关标签:
3条回答
  • 2020-12-11 22:36

    You're combining procedural and object oriented approaches, when you only want object oriented. Change

    $num=mysqli_num_rows($WHAT);
    

    to

    $num = $stmt->num_rows();
    
    0 讨论(0)
  • 2020-12-11 22:36

    The reason for this error is that you may be passing the $stmt object to mysqli_num_rows().

    Moreover, what I see is that you're mixing up two different approaches in php for using mysql databases - the Object-Oriented Approach and the Procedural Approach. You have to chose one of them and then proceed something like this -

    Procedural Approach (Using mysqli extension)

    $con = mysqli_connect("localhost", "user", "pass", "test") or die("Connection Error: " . mysqli_error($conn));
    $query = "SELECT id, name, status, type FROM organization"; 
    $result = mysqli_query($con, $query) or die(mysqli_error($con));
    $count = mysqli_num_rows($result);
    
    // $count now stores the number of rows returned from that query
    

    Object-Oriented Approach (Using mysqli extension)

    <?php
    $host = "localhost";
    $username = "user";
    $password = "pass";
    $dbname = "test";
    
    // Create connection
    $conn = new mysqli($host, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $sql = "SELECT id, name, status, type FROM organization";
    $result = $conn->query($sql);
    $count = $result->num_rows;
    // $count now stores the number of rows returned
    $conn->close();
    ?>
    

    You can read more about it here - https://www.w3schools.com/php/php_mysql_select.asp

    0 讨论(0)
  • 2020-12-11 22:51

    mysqli_num_rows takes the query result as a parameter http://us.php.net/manual/en/mysqli-result.num-rows.php

    You could also use it in an OOP style as $result->num_rows;

    0 讨论(0)
提交回复
热议问题