Warning: mysqli_query() expects parameter 1 to be mysqli, null given in

前端 未结 3 1187
醉话见心
醉话见心 2020-11-22 11:49

I am trying to build a simple custom CMS, but I\'m getting an error:

Warning: mysqli_query() expects parameter 1 to be MySQLi, null given in

3条回答
  •  眼角桃花
    2020-11-22 12:19

    As mentioned in comments, this is a scoping issue. Specifically, $con is not in scope within your getPosts function.

    You should pass your connection object in as a dependency, eg

    function getPosts(mysqli $con) {
        // etc
    

    I would also highly recommend halting execution if your connection fails or if errors occur. Something like this should suffice

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // throw exceptions
    $con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");
    
    getPosts($con);
    

提交回复
热议问题