Problem description
I just started to learn PHP, and the system environment is Ubuntu+PHP7.0+Mysql5.7+Apache2.
running a database connection test sample error:
[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/test.php on line 2
The sample code is:
<?PHP
$conn=mysql_connect("localhost","root","root");
if($conn){
echo"ok";
}else{
echo"error";
}
?>
The solution
The mysql_connect() function has been deprecated since PHP5.0. In PHP 7.0 it has been deprecated and replaced with this function:
mysqli_connect();
Usage is:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
The description of the official connection: http://php.net/manual/en/function.mysqli-connect.php
the correct test code:
<?PHP
$conn=mysqli_connect("localhost","root","root");
if($conn){
echo"ok";
}else{
echo"error";
}
?>
conclusion
- in the Ubuntu + PHP7.0 + Mysql5.7 + Apache2 system under the environment of the wrong, because the mysql_connect () function has been deprecated, when following outdated tutorial learning may encounter this error. (note: if it is a Windows system, are more likely to be Apache2 not enable mysql, details on baidu) when running the above test code, interface without any reaction, the error is in the log to consult, log directory in the/var/log/Apache2/error log “.
Read More:
- Fatal error: Uncaught Error: Call to undefined function mysql_connect()
- Call to undefined function mysql_ Connect() solution summary
- PHP connection PostgreSQL error call to undefined function PG_ connect()
- Fatal error: Uncaught Error: Call to undefined function mysql_ Connect() problem solving
- Uncaught error: call to undefined function MySQL when building sqli lab environment with phpstudy_ Connect() error
- Uncaught Error: Call to undefined function mysql_select_db()
- MySQL: if the remote connection using navicatip fails, prompt “is not allowed to connect to this MySQL server”
- node.js Server MySQL database connection timeout (error: connect etimeout)
- PHP error: call to undefined function curl_ init() window
- Connection between PHP 7.4 and MySQL (MariaDB) under Ubuntu (kali Linux)
- Call to undefined function oci_ Connect() problem solving
- java.lang.ClassNotFoundException : com.mysql.jdbc . driver project cannot connect to database, exception, lack of MySQL driver
- MySQL local connection Error 1130_ The solution of MySQL 1130 error report when remote connection through Navicat for MySQL
- It can’t connect to local MySQL server through socket ‘/ tmp/ mysql.sock ‘(2) “;
- MySQL Workbench Failed to Connect to MySQL at 127.0.0.1:3306 with user root Bad handshake
- The MySQL service suddenly hangs up with the error message can’t connect to MySQL server on ‘localhost’ (10061)
- MySQL Workbench: Error Code: 2013. Lost connection to MySQL server during query solution
- JDBC connect to Sql Server to connect to the database–The TCP/IP connection to the host localhost, port 1433 has failed
- Navicat for MySQL error 1251 failed to connect to MySQL
- php mysqli_ connect_ Error () and mysqli_ connect_ The difference of errno() function