Home  >  Article  >  Backend Development  >  What should I do if php cannot connect to the database?

What should I do if php cannot connect to the database?

藏色散人
藏色散人Original
2021-03-18 10:26:005255browse

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.

What should I do if php cannot 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(&#39;Could not connect: &#39; . 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn