Home  >  Article  >  Operation and Maintenance  >  How to connect to local database when building a website in phpstudy

How to connect to local database when building a website in phpstudy

下次还敢
下次还敢Original
2024-04-02 14:09:19496browse

How to use PHP Study to build a website and connect to a local database

Step 1: Connect to the database server

PHP Study has a built-in MySQL service, the default port number is 3306. Open the PHP Study control panel and make sure the MySQL service is running.

Step 2: Create a database

In the PHP Study control panel, click the "Database" tab, and then click the " " button to create a new database. Enter the database name and click OK.

Step 3: Create a database user

Click the "User" tab, and then click the " " button to create a new user. Enter your username, password, and host (usually "localhost"). Then select the Grant all permissions checkbox and click OK.

Step 4: Import data

If you already have a database backup, you can click the "Import" button and select the backup file. If you don't have a backup, you can skip this step.

Step 5: Configure PHP website

In the PHP Study control panel, click the "Website" tab, and then click the " " button to create a new website. Enter the path to the website root directory.

Step 6: Connect to the database

In the website root directory, create the following PHP file (such as database-connection.php):

<code class="php"><?php
// 数据库连接参数
$servername = "localhost";
$username = "数据库用户名";
$password = "数据库密码";
$dbname = "数据库名称";

// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);

// 检查连接
if (!$conn) {
    die("连接失败:" . mysqli_connect_error());
}
echo "连接成功!";
?></code>

Step 7: Test the connection

Open a browser and visit the website, such as http://localhost/database-connection.php. If the connection is successful, you should see the "Connection successful!" message.

The above is the detailed content of How to connect to local database when building a website in phpstudy. 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