search
HomeBackend DevelopmentPHP7php7 cannot connect to mysql

php7 cannot connect to mysql

Oct 21, 2019 pm 04:36 PM
mysqlphp7connect

php7 cannot connect to mysql

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function