简体   繁体   English

php本地连接mysql数据库

[英]php local connection mysql database

I try to make a simple IOS app that can connect to mysql database and read one table. 我尝试制作一个可以连接到mysql数据库并读取一个表的简单IOS应用程序。 But my php code does't work and really have no idea why, it's seems correct to me. 但是我的php代码不起作用,并且实际上不知道为什么,这对我来说似乎是正确的。 The database is in a raspberry phpmyadmin server and the server works great. 该数据库位于树莓派phpmyadmin服务器中,该服务器运行良好。 I will put my code here and please tell me what's wrong. 我将把代码放在这里,请告诉我怎么了。

<?php

$host = "192.168.2.193";
$db = "produtos";
$user = "root";
$pass = "1234";

$connection = mysql_connect($host, $user, $pass);

if(!$connection)
{
die("Database server connection failed.");
}
else
{
//attempt to select the database
$dbconnect = mysql_select_db($db, $connection);

//check to see if we could select the database
if(!dbconnect)
{
       die("Unable to connect to the specified database!");
}
else
{
    $query = "SELECT * FROM produtos";
    $resultset = mysql_query($query, $connection);

    $records = array();

    //loop throught all our records and add them to our array

    while ($r = mysql_fetch_assoc($resultset))
    {
        $records[] = $r;
    }

    echo json_ecode($records);
    echo $resultset; 

}
}

?>

Based on the question: 根据问题:

use mysqli_connect rather than mysql_connect because mysql_connect is deprecated and will not work someday. 请使用mysqli_connect而不是mysql_connect因为mysql_connect已过时,并且有朝一日将无法使用。 Also what is the the error you are getting? 另外,您得到的错误是什么? change your die() statement to something more helpful die(mysqli_error($connection)); 将您的die()语句更改为更有用的方法die(mysqli_error($connection));

Based on your comment: 根据您的评论:

That error would suggest that you either A) don't have the right IP address or B) there is a network issue between your host server and the SQL server, is this code running on the same server that is hosting the SQL database? 该错误提示您要么A)没有正确的IP地址,要么B)主机服务器和SQL服务器之间存在网络问题,此代码是否在托管SQL数据库的同一服务器上运行? if so then you can probably just use localhost for your $host 如果是这样,那么您可能只需将localhost用于$ host

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM