Home  >  Article  >  Database  >  Detailed steps for connecting php to mysql database (pictures and texts)

Detailed steps for connecting php to mysql database (pictures and texts)

迷茫
迷茫Original
2017-04-07 16:00:3223566browse

Introduction to mysql:

Mysql is a popular database. Because it is an open source semi-commercial software, it has a high market share and is favored by PHP developers. It has always been considered the most popular database in PHP. best partner. At the same time, PHP also has strong data support capabilities.

Detailed steps for connecting php to mysql database (pictures and texts)

PHP Link to MYSQL Steps

Database Server

To connect to the database server, we need to use mysql_connect()

Syntax: mysql_connect(parameter 1, parameter 2, parameter 3);

Let’s explain a few in detail below What do the parameters mean.

Parameter 1: In the local operating environment, localhost is enough.

Server environment, then you need to enter the host name or IP of the server

Parameter 2: Local or server side , Database user name

Parameter 3: Local or server side, database password

The return value of this function indicates the link to this database

mysql_connect("localhost", "root","root") or die("Failed to connect to the database or the server has not started");

We can see from the above function that you can use the local machine name as the database server, so This provides guarantees for the off-site storage of data and the security isolation of the database.

We create an index.php file under the local server to test whether the link is successful.

The code in index.php

<?php
$con = mysql_connect("localhost","root","root")or die("链接数据库失败或者服务器没有启动");
if($con){
echo "数据源链接成功";
}
?>

Then we run the index.php file. If there is output, then our link is successful.

Detailed steps for connecting php to mysql database (pictures and texts)

In this way, we have a successful link. After the database server connection is successful, we need to connect to the database.

Linked database

Now let’s look at our graphical management tools

Detailed steps for connecting php to mysql database (pictures and texts)

Look at the picture above, we have a database named study with a table in it.

Now we want to link to the study database, we need to use mysql_select_db()

Let’s see how this is used

mysql_select_db('study');

We also need to use or die() to see if the database connection is successful.

Detailed steps for connecting php to mysql database (pictures and texts)

We don’t have the studies database, let’s run it and see what the effect will be.

Detailed steps for connecting php to mysql database (pictures and texts)

A prompt message appears, indicating that we do not have this database. Change studies to study, and a blank appears, indicating that we have successfully linked the data.

The above is the detailed content of Detailed steps for connecting php to mysql database (pictures and texts). 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