PHP Hint Deprecated: mysql_connect of: The mysql extension is deprecated Solution

  • 2021-07-16 02:02:55
  • OfStack

In this paper, the solution of PHP prompting Deprecated: mysql_connect (): The mysql extension is deprecated is described with examples. This kind of problem is often encountered in PHP program development. Share it for your reference. The specific solutions are as follows:

Change the following code to mysqli or PDO.


function connectit () { 
global $CFG; 
mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error()); 
mysql_select_db($CFG['db_name']); 
} 

PDO:


$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); 

MYSQLI:


$link = mysqli_connect( 
 'localhost', /* The host to connect to  Connect MySQL Address  */   
 'user',   /* The user to connect as  Connect MySQL User name  */   
 'password', /* The password to use  Connect MySQL Password  */   
 'world');  /* The default database to query  Connection database name */   

if (!$link) { 
  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error()); 
  exit; 
}

I hope this article is helpful to everyone's PHP programming.


Related articles: