如何使用PHP从MySQL获取数据
大家好,我正在尝试构建IOS应用程序,我需要从MySQL数据库获取数据。我不懂php。我找到了一本教程 在创建PHP服务的第3节中,我复制并编辑了它,以获取我的信息。PHP代码是这样的如何使用PHP从MySQL获取数据,php,mysql,Php,Mysql,大家好,我正在尝试构建IOS应用程序,我需要从MySQL数据库获取数据。我不懂php。我找到了一本教程 在创建PHP服务的第3节中,我复制并编辑了它,以获取我的信息。PHP代码是这样的 <?php //create connection $con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1"); // Check connection if (mysqli_connect_errno()) {
<?php
//create connection
$con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1");
// 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 treelibrary";
// 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);
?>
我把它装入我的服务器,没有东西弹出只是空白页…有什么原因吗?我的错误在哪里。谢谢你的帮助。
祝您有愉快的一天。我没有阅读您的全部代码,但我非常确定您正在尝试将结果转换为JSON格式并进行响应 这里有一个简单的方法
<?php
//
// EDIT: dont use this code, scroll a little bit lower to see the edit I made.
//
//create connection
$con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1");
// 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 treelibrary";
// Executes the SQL statement and puts results into $res
$res = $con->query($sql);
// Checks if there's any rows
if($res->num_rows > 0) {
// Puts all results in $row
$row = $res->fetch_assoc();
// echo & encode datas
echo json_encode($row);
} else {
echo "no data found";
}
// Close connections
mysqli_close($con);
看起来只有在执行查询成功时才输出一些内容。如果查询因某种原因失败,则永远不会到达echo
语句。您是否可以添加一个else
条件,在该条件下您echo mysqli\u错误($con)代码>(仅用于测试和调试目的)?您的树库似乎没有任何记录。@rickdenhaan我写了它,但什么都没有发生只是一个空白页again@Chayan我的电脑里有一张唱片table@MertalpTaşdelen我用我的数据测试了你的代码,它是对的。检查是否启用mysqli我现在尝试了您的代码,但结果可能相同,可能是my$con=mysqli\u connect(“localhost”、“myuserid”、“mypassword”、“i4142489\u wp1”)代码>行错误>代码>将所有结果放入$$/CODE >是不正确的,只将第一行插入。谢谢@ USER 783243修复它。@ USER 783243在使用编辑版本的代码RESUL之后是空白页,那么可能是您的错误,或者您的数据库中没有内容,您的数据库信息的错误或您有一些其他代码正在阻止它。。。因为我又试了一次,它仍然有效。。。某些内容应该总是打印在屏幕上,因为有IF&ELSE语句,并且两者都有echo()
。如果你有不和谐,你可以留下你的不和谐标签,我会打电话给你寻求进一步的帮助。
<?php
//create connection
$con=mysqli_connect("localhost","myuserid","mypassword","i4142489_wp1");
// 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 treelibrary";
// Executes the SQL statement and puts results into $res
$res = $con->query($sql);
// Checks if there's any rows
if($res->num_rows > 0) {
// defines $data
$data = array();
// grabs all data and adds them to the $data array
while ($row = $res->fetch_assoc()) {
array_push($data, $row);
}
// echo & encode datas
echo json_encode($data);
} else {
echo "no data found";
}
// Close connections
mysqli_close($con);