试图让MySQL通过php连接到JSON,但从php脚本连接到MySQL时返回错误
本质上,我试图用JSON打印信息,以便与我的应用程序通信,但由于一些奇怪的原因,我无法从php脚本连接到MySQL数据库。导致错误的原因可能是什么: 警告:mysql_connect()[function.mysql connect]:在第13行的/srv/disk11/1158855/www/(myphpwebsite)/lib.php中查询时与mysql服务器失去连接 无法连接:在查询过程中与MySQL服务器失去连接 另外,第13行表示lib.php中的行:试图让MySQL通过php连接到JSON,但从php脚本连接到MySQL时返回错误,php,mysql,json,connection,Php,Mysql,Json,Connection,本质上,我试图用JSON打印信息,以便与我的应用程序通信,但由于一些奇怪的原因,我无法从php脚本连接到MySQL数据库。导致错误的原因可能是什么: 警告:mysql_connect()[function.mysql connect]:在第13行的/srv/disk11/1158855/www/(myphpwebsite)/lib.php中查询时与mysql服务器失去连接 无法连接:在查询过程中与MySQL服务器失去连接 另外,第13行表示lib.php中的行: mysql\u connect(
mysql\u connect($dbhost,$dbuser,$dbpass)或die(“无法连接:”.mysql\u error())代码>
还应该注意的是,如果有人想要追踪来源,这是对前面问题的后续
最后,我从使用mysql的本地主机和远程服务器上得到了相同的错误
lib.php
<?
//Database Information
$dbhost = "31.170.160.76";
$dbname = "testdatabase";
$dbuser = "(personalinformation)";
$dbpass = "tested123";
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass) or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
//executes a given sql query with the params and returns an array as result
function query() {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}
//loads up the source image, resizes it and saves with -thumb in the file name
function thumb($srcFile, $sideInPx) {
$image = imagecreatefromjpeg($srcFile);
$width = imagesx($image);
$height = imagesy($image);
$thumb = imagecreatetruecolor($sideInPx, $sideInPx);
imagecopyresized($thumb,$image,0,0,0,0,$sideInPx,$sideInPx,$width,$height);
imagejpeg($thumb, str_replace(".jpg","-thumb.jpg",$srcFile), 85);
imagedestroy($thumb);
imagedestroy($image);
}
?>
Index.php
<?
session_start();
require("lib.php");
require("api.php");
header("Content-Type: application/json");
switch ($_POST['command']) {
case "login":
login($_POST['username'], $_POST['password']); break;
case "register":
register($_POST['username'], $_POST['password']); break;
}
exit();
?>
api.php
<?php
function errorJson($msg){
print json_encode(array('error'=>$msg));
exit();
}
function register($user, $pass) {
//check if username exists
$login = query("SELECT username FROM login WHERE username='%s' limit 1", $user);
if (count($login['result'])>0) {
errorJson('Username already exists');
//try to register the user
$result = query("INSERT INTO login(username, pass) VALUES('%s','%s')", $user, $pass);
if (!$result['error']) {
//success
login($user, $pass);
} else {
//error
errorJson('Registration failed');
}
}
}
function login($user, $pass) {
$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);
if (count($result['result'])>0) {
//authorized
$_SESSION['IdUser'] = $result['result'][0]['IdUser'];
print json_encode($result);
} else {
//not authorized
errorJson('Authorization failed');
}
}
?>
由于这是在“连接”线上,服务器已经找到(否则您会收到另一条消息),但您尚未协商登录
直截了当地说:
更罕见的是,当客户端尝试与服务器进行初始连接时,会发生这种情况。在这种情况下,如果您的connect_timeout值设置为仅几秒,则可以通过将其增加到10秒来解决问题,如果您的连接距离很长或连接速度很慢,则可能会更多
如果不是这样,那可能是网络问题,或者您的连接在身份验证过程中被终止。检查您的mysql主机是否没有一些奇怪的验证,证明您来自某个特定的IP(我说奇怪,因为管理它的标准方法比终止身份验证中间流多),或者从更接近mysql服务器的服务器上尝试您的PHP脚本(在网络速度方面更接近).我弄明白是怎么回事了。事实证明,我的php代码与登录名冲突。这就是为什么它不会在多个远程MySQL和我自己的本地主机上进行身份验证的原因显示的错误消息是什么?警告:MySQL_connect()[function.MySQL connect]:在/srv/disk11/1158855/www/(myphpwebsite)中查询时与MySQL服务器失去连接/第13行的lib.php无法连接:在查询过程中与MySQL服务器失去连接如果您指出您正在运行的许多查询中的哪一个实际触发了错误,这会很有帮助。您搜索过该错误消息吗?很多建议…错误消息是由lib.php触发的。是的,我在谷歌上搜索过,但我相信这是我的一个特例。不确定可能是什么…欢迎任何建议!谢谢你的回答,但是没有用。我在本地服务器和2台远程服务器上都试过了…所有服务器上都出现了相同的错误…你设置了什么时间间隔?设置更改后,您是否重新启动了MySQL服务器?如您所说,将它们设置为10秒(这应该足够了),我在尝试之前确实重新启动了所有这些服务器…并且我和远程服务器的连接正常。我的代码有什么问题吗?还有其他建议吗?没有-代码没有问题。这在多个SQL服务器上发生?你能试着在命令行连接吗:“mysql-h31.170.160.76-u[username here]-p”,因为这让我知道我不允许连接。。。