【发布时间】:2015-03-30 06:53:54
【问题描述】:
我正在尝试在我的网站上托管此 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/
【问题讨论】:
-
在不属于您的网站上测试 php 将不允许您连接到 /the/their 的数据库。如果您想测试服务器端脚本,我建议您安装 wamp 服务器。我发现很难理解您的问题,因为您似乎在问题中回答了它。我希望这篇文章有所帮助。