PHP警告:mysqli_close()期望参数1为mysqli [英] php warning: mysqli_close() expects parameter 1 to be mysqli

查看:409
本文介绍了PHP警告:mysqli_close()期望参数1为mysqli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过php连接到sql db,并不断收到我无法弄清的错误.我可以毫无错误地连接到另一个调试脚本.我得到了连接并提取了我的数据,但最后出现了一个错误.

I am attempting a connection to a sql db via php and keep getting an error I can't figure out. I can connect with another debug scripts with no errors. I get my connection and pull my data but pulls an error at the end.

$con=mysqli_connect("localhost","username","password","dbname");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();

    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}

// Close connections
mysqli_close($result);
mysqli_close($con);
?>

带来这个

[{"Name":"Apple","Address":"1 Infinity Loop Cupertino, CA","Latitude":"37.331741","Longitude":"-122.030333"},{"Name":"Googleplex","Address":"1600 Amphitheatre Pkwy, Mountain View, CA","Latitude":"37.421999","Longitude":"-122.083954"}]
Warning: mysqli_close() expects parameter 1 to be mysqli, object given in /home/jfletch/public_html/appone/connect.php on line 36

任何方向都值得赞赏.

Any direction is appreciated.

推荐答案

mysqli_close($result);

上面的行不正确.您只需要调用一次mysqli_close()(如果有必要,则完全可以这样做,正如注释中指出的那样,在脚本执行结束时关闭连接),并且该参数应该是您的链接标识符,而不是您的查询资源.

The line above is incorrect. You only need to call mysqli_close() once (if at all since, as pointed out in the comments, the connection is closed at the end of the execution of your script) and the parameter should be your link identifier, not your query resource.

删除它.

这篇关于PHP警告:mysqli_close()期望参数1为mysqli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆