1
votes

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"

2
if you run phpInfo(); then you will see a lot of information about PHP, version etc. and what drivers are loaded etc. - Ryan Vincent
@Ryan phpInfo() shows: PHP version is 5.3.0 and " MYSQL Client API version : 4.0.31" - Lefteris
try to omit the port on your host path: define('DB_HOST', 'mysql.utk.edu'); I had a similar problem recently and solved this way - Fabio Gonzaga

2 Answers

0
votes

The function mysql_connect is deprecated.

0
votes

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='';