問題描述
我正在嘗試通過 php 連接到 sql 數(shù)據(jù)庫,并不斷收到我無法弄清楚的錯(cuò)誤.我可以連接另一個(gè)調(diào)試腳本而沒有錯(cuò)誤.我獲得了連接并提取了我的數(shù)據(jù),但最后卻出現(xiàn)了錯(cuò)誤.
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);
?>
把這個(gè)拿出來
[{名稱":蘋果",地址":1 Infinity Loop Cupertino,CA",緯度":37.331741",經(jīng)度":-122.0303333""},{"Name":"Googleplex","Address":"1600 Amphitheatre Pkwy, Mountain View, CA","Latitude":"37.421999","Longitude":"-122.083954"}]
[{"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"}]
警告:mysqli_close() 期望參數(shù) 1 是 mysqli,對(duì)象在/home/jfletch/public_html/appone/connect.php 第 36 行
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);
上面的行是不正確的.您只需要調(diào)用 mysqli_close()
一次(如果有的話,正如評(píng)論中指出的那樣,連接在腳本執(zhí)行結(jié)束時(shí)關(guān)閉)并且參數(shù)應(yīng)該是您的鏈接標(biāo)識(shí)符,而不是您的查詢資源.
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.
刪除它.
這篇關(guān)于php 警告:mysqli_close() 期望參數(shù) 1 是 mysqli的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!