简体   繁体   中英

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. The form tries to connect to the database with the following script: (EDITED following davejal suggestions)

 <?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; consider upgrading MySQL client

output when I click "submit" the form

As you see the mysqli doesn't exist.

I have tried to change the password to the old encryption, still get the error (phpmyadmin screenshot): change to OLD password

2nd EDIT. phpinfo() shows PHP version 5.3.0 and "Client API version : 4.0.31"

不建议使用mysql_connect函数。

on the line where you declare $con change to mysqli so

$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 (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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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