Home Backend Development PHP Problem How to connect php file to database

How to connect php file to database

Sep 18, 2019 pm 01:04 PM
php database connect

How to connect php file to database

How to connect to the database in PHP

The first step is to create a connection

$a = mysql_connect("localhost","root","");
Copy after login

The meaning of the parameters in the brackets:

1.localhost - IP address, localhost means the local machine. If you need to connect to another server, you need to enter the IP address of the server you want to connect to

2.root——User name, the user name of the server to be connected to, just write the user name saved when creating the server

3.""——Server password, the server to be connected to Password, if there is no password, write nothing within the double quotes, but the double quotes cannot be omitted

4.$a - If the connection is successful, a MySQL connection ID will be returned or returned when the connection fails. A FALSE, since there is a return value, we need to create a variable to receive it.

The second step is to select the database to be operated

mysql_select_db("mydb",$a);
Copy after login

mydb——the name of the database

$a——what to use to connect to the database

The third step, write the SQL statement

$q = "select * from Nation"
Copy after login

Query all the data in the Nation table

The fourth step, execute the SQL statement

$w=mysql_query($q);
Copy after login

Call the mysql_query() method to execute this SQL statement. When it is executed, a result set will be returned. We need to create a variable to receive it.

$q——The variable representing the SQL statement

$w——The variable that accepts the result set

The fifth step is to read from the result set Fetching data

The result set returned can be read using the mysql_fetch_xxx method.

while($e = mysql_fetch_row($w))
{
  var_dump($e);
}
Copy after login

Note: When creating the database, select utf-8 as the encoding format.

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to connect php file to database. 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 Article

Hot Article

Hot Article Tags

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

CakePHP Creating Validators

See all articles