Problem description
I just started learning php, and the system environment is Ubuntu + PHP7.0 + Mysql5.7 + Apache2.
An error occurred while running a database connection test example:
[client 127.0.0.1:37496] PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/test.php:2\nStack trace:\n#0 {main}\n thrown in /var/www/html/ on line 2
The sample code is:
<?PHP
$conn=mysql_connect("localhost","root","root");
if($conn){
echo"ok";
}else{
echo"error";
}
?>
Solution
After reviewing the information, I found that it was not recommended to use the mysql_connect() function since PHP5.0. When it comes to php7.0, the function is directly discarded. The replacement function is:
mysqli_connect();
Usage is:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
Official description link:/manual/en/
Correct test code:
<?PHP
$conn=mysqli_connect("localhost","root","root");
if($conn){
echo"ok";
}else{
echo"error";
}
?>
Summarize
- This error is reported in the system environment of Ubuntu+PHP7.0+Mysql5.7+Apache2 because the mysql_connect() function is deprecated. You may encounter this error when learning with outdated tutorials. (Note: If it is a Windows system, it is more likely that Apache2 does not enable mysql. Please use Baidu for details)
- When running the above test code, there is no response on the interface. The error is found in the log, and the log directory is "/var/log/apache2/".