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

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

Apr 02, 2024 pm 02:09 PM

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):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?php

// 数据库连接参数

$servername = "localhost";

$username = "数据库用户名";

$password = "数据库密码";

$dbname = "数据库名称";

 

// 创建连接

$conn = mysqli_connect($servername, $username, $password, $dbname);

 

// 检查连接

if (!$conn) {

    die("连接失败:" . mysqli_connect_error());

}

echo "连接成功!";

?>

Copy after login

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 of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)