PHP 警告 : mysqli_close() expects parameter 1 to be mysqli

标签 php mysqli

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

$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

最佳答案

mysqli_close($result);

上面这行是不正确的。您只需要调用 mysqli_close() 一次(如果有的话,正如评论中指出的那样,连接在脚本执行结束时关闭)并且参数应该是您的链接标识符,而不是您的查询资源。

删除它。

关于PHP 警告 : mysqli_close() expects parameter 1 to be mysqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23282284/

相关文章:

php - 弹出此错误时如何在 Windows Xampp 上安装 Subrion

php - 不确定为什么我得到 : Number of variables doesn't match number of parameters in prepared statement

php - 将 cURL 发布到 Zapier Webhook

php - MYSQL 即使我在 php.ini 中定义,连接也超时

php - 发布的值没有进入 json highcharts-php 和 mysql 的 where 子句

javascript - 无法从 PHP 读取 Javascript responseXML

php - 如何获取mysqli中那些相互跟随的记录?

php - 我应该如何处理用户名已被使用的情况?

PHP:如何检查查询字符串或 POST 变量是否两次包含相同的变量

php - 重定向出目录