我试图在我的网站上托管这个php文件以连接到MySQL数据库。
<?php
// Create connection
$con=mysqli_connect("localhost","username","pass","database");
// 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($con);
?>
当我尝试使用http://writecodeonline.com/php/验证代码时,它会说
致命错误:在第2行调用未定义的函数mysqli_connect()
搜索周围说我需要更改php.ini文件,但我甚至没有托管在我的网站上。代码有问题吗?
当我尝试在www.mydomain.com/service.php上访问它时,它说文件未找到错误......但它肯定存在。我正在使用本教程 - http://codewithchris.com/iphone-app-connect-to-mysql-database/
答案 0 :(得分:1)
您需要在服务器上安装php-mysql依赖项。
yum install php-mysql -y
或等同于你的操作系统。