php mysql connection fails because higher versions of php no longer use "mysql_connect()" to connect to the MySQL database. The solution is to use mysqli or PDO to replace "mysql_connect" and reconnect to mysql.
Recommended: "PHP Tutorial"
PHP cannot connect to MySQL and failed
Higher versions of php no longer use mysql_connect() to connect to the MySQL database.
<?php $con = mysql_connect("localhost","root","abc123"); if (!$con) { die('连接失败' . mysql_error()); } ?>
Today I used the above code to link to my database, but it failed. Tip me:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in D:\Sian\Blog pages\demo.php on line 2
mysql_connect() has been deprecated and will be deleted in the future. Use mysqli or PDO to replace it.
Then the PHP official website explains it this way:
mysql_connect - Open a connection to the MySQL server
Warning
This extension is available since PHP 5.5.0 Deprecated and removed starting with PHP 7.0.0. It should be replaced with the MySQLi or PDO_MySQL extension. See the MySQL: Selection API guide and related FAQ for more information.
The alternatives to this function are:
mysqli_connect() PDO::__construct()
The above is the detailed content of What to do if php mysql connection fails. For more information, please follow other related articles on the PHP Chinese website!