简体   繁体   English

MySQL:连接到远程数据库错误

[英]MySQL: Connect to a remote Database error

I have a simple HTML form that that tries to connect to a database hosted on a remote server. 我有一个简单的HTML表单,该表单试图连接到远程服务器上托管的数据库。 The form tries to connect to the database with the following script: (EDITED following davejal suggestions) 该表单尝试使用以下脚本连接到数据库:(根据davejal建议进行编辑)

 <?php
 ini_set('display_errors', 1);
 ini_set('log_errors', 1);
 ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
 error_reporting(E_ALL);
 $error='';

 echo "<p>Test</p>" ;
 define('DB_HOST', 'mysql.utk.edu:3306');
 define('DB_NAME', 'emoschan_test');
 define('DB_USER','emoschan');
 define('DB_PASSWORD','NewPass');

echo "   Does mysql  exist?". var_dump(function_exists('mysql_connect'));
echo "   Does mysqli exist?". var_dump(function_exists('mysqli_connect'));
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Failed to connect to MySQL: " . mysql_error());

if (mysql_connect_error($con)) { echo "Third Failed to connect to MySQL:" . mysql_connect_error();  }
else { echo "<p>Successfully connected to your database</p>"; }
?>

The connection fails and I receive the followin error ( I include a screenshot of the error): 连接失败,我收到跟随错误(我包括错误的屏幕截图):

Failed to connect to MySQL: Client does not support authentication protocol requested by server; 无法连接到MySQL:客户端不支持服务器请求的身份验证协议。 consider upgrading MySQL client 考虑升级MySQL客户端

output when I click "submit" the form 当我单击“提交”表单时输出

As you see the mysqli doesn't exist. 如您所见,mysqli不存在。

I have tried to change the password to the old encryption, still get the error (phpmyadmin screenshot): change to OLD password 我试图将密码更改为旧加密,仍然收到错误(phpmyadmin屏幕截图): 更改为OLD password

2nd EDIT. 第二次编辑。 phpinfo() shows PHP version 5.3.0 and "Client API version : 4.0.31" phpinfo()显示PHP版本5.3.0和“客户端API版本:4.0.31”

不建议使用mysql_connect函数。

on the line where you declare $con change to mysqli so 在您声明$con更改为mysqli的行上

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Failed to connect to MySQL: " . mysql_error());

becomes: 变成:

$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Failed to connect to MySQL: " . mysql_error());

You can remove the line where you select the db, because you already select it in when creating the connection, by passing it as the fourth variable. 您可以删除选择数据库的行,因为在创建连接时已经选择了它,方法是将其作为第四个变量传递。

Don't forget to change the if part also to become: 不要忘记将if部分也变成:

if (mysqli_connect_error($con)) { echo "Third Failed to connect to MySQLi:" . mysqli_connect_error();  }

to display all errors add the following right after opening php 要显示所有错误,请在打开php后添加以下内容

 ini_set('display_errors', 1); 
 ini_set('log_errors', 1); 
 ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
 error_reporting(E_ALL);
 $error='';

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

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