Home > Article > Operation and Maintenance > How to connect to local database when building a website in phpstudy
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!