
1. Add the php.ini file
In the new version of PHP7, there is no php.ini, so you need to create it yourself. The new version has two php.ini-development and php.ini-production files. Just copy one and rename it to php.ini.
2. Modify the content of the php.ini file
Generally, such files have access permission restrictions. Right-click the php.ini file and modify the operation permissions of the file. , writing is allowed.
To connect to MySQL, remove the semicolon in front of extension=pdo_mysql. To connect to other databases, you also need to remove the corresponding semicolon.
Then On windows: extension_dir = “ext” fill in the address here completely, such as extension_dir = “C:\Program Files\PHP\ext”.
3. Use PDO to connect (mysqli is also available)
The Mysql_connect module is no longer available. The new version needs to use PDO or mysqli to connect.
<?php
$servername = "localhost";
$username = "root";
$password = "your password";
try {
$conn = new PDO("mysql:host=$servername;dbname=test;port=3306", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>Open the php file, if "Connection successful" is displayed.
Attachment: The php file must be opened on the server. You can enter: php -S localhost:port number in the command line cmd, you can Run a virtual server that comes with PHP on your PC, and then you can open a browser to access it. You only need to enter localhost:port number in the address bar.
Recommended tutorial: PHP video tutorial
The above is the detailed content of php7 cannot connect to mysql. For more information, please follow other related articles on the PHP Chinese website!