Home > Article > Backend Development > What should I do if php cannot connect to the database?
php cannot connect to the database because PHP7 has abandoned the connect method and cannot connect to the database. The solution is to use the mysqli method to connect to the database.
The operating environment of this article: Ubuntu16 system, PHP7.0 version, DELL G3 computer
PHP cannot connect to the database?
I used the connect method to connect to the database successfully, but now it fails. The code is as follows:
<?php $con = mysql_connect("localhost","root","code"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_close($con); ?>
My Ubuntu is version 16 and PHP is 7.0. Finally, I found that PHP7 has abandoned the connect method and used PDO and mysqli instead. I switched to the mysqli method and it was successful. For specific operations, please see the tutorial below:
PDO:
https://www.php.cn/php/php-pdo .html
Mysqli:
https://www.php.cn/php/php-ref-mysqli.html
The above is the detailed content of What should I do if php cannot connect to the database?. For more information, please follow other related articles on the PHP Chinese website!