Fatal error: Call to a member function fetch_assoc() on null on line 9

后端 未结 2 1704
生来不讨喜
生来不讨喜 2021-01-29 14:54

I don\'t understand

Не понимаю, в чем заключается ошибка

query(\"SET         


        
相关标签:
2条回答
  • 2021-01-29 15:33

    you can try like this....you have to fetch that array and you can print....

    <?php
    $mysqli = new mysqli ('localhost', 'root', '', 'db_name');
    $mysqli->query("SET NAMES 'utf-8'");
    $result_set= mysqli_query($mysqli,"SELECT * FROM products");
    $row=mysqli_num_rows($result_set);
    echo $row;
    while($row=mysqli_fetch_array($result_set))
    {            
    print_r($row);
    }
    ?>
    
    0 讨论(0)
  • 2021-01-29 15:33
    1. The $result_set is outside the function scope. You can't access it within the function.
    2. You're selecting from a string not a table name

    Try the following changes:

    <?php
    
    
    
    function printResult () {
        $mysqli = new mysqli ('localhost', 'root', '', 'UTS');
        $mysqli->set_charset("utf-8"); //Better to do it this way
        $result_set = $mysqli->query ("SELECT * FROM `product`"); //Not the ` instead of '
    
        while (($row = $result_set->fetch_assoc()) != false) {
            print_r ($row);
            echo "<br/>";
        }
        $mysqli -> close();
    }
    

    You can run this function from anywhere.

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