php警告:mysqli_close()期望参数1为mysqli

时间:2014-04-25 00:41:49

标签: php sql json

我正在尝试通过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

任何方向都表示赞赏。

1 个答案:

答案 0 :(得分:10)

mysqli_close($result);

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

删除它。