Home > Database > Mysql Tutorial > php connect to database

php connect to database

巴扎黑
Release: 2016-11-11 17:59:10
Original
1405 people have browsed it


MySQL connection and management

Let PHP support MySQL

PHP has a proprietary MySQL function library to operate MYSQL database.

MySQL is no longer supported by default in PHP 5 and later versions, so before running these libraries, please make sure php.ini is loaded with MySQL database support:

extension = mysql.dll

MySQL connection

mysql_connect() function Used to open a connection to the MySQL database.

Syntax:

mysql_connect(servername, username, password)

Although the above three parameters are optional, usually we need to specify them clearly.

Example of testing database connection:

<?php
$conn = @mysql_connect("localhost","root","root1234");
if (!$conn){
    die("连接数据库失败:" . mysql_error());
} else {
    echo "连接数据库成功!";
}
?>
Copy after login

In this example, the @ operator is used to mask the error message when the mysql_connect() function fails to connect and replace it with a custom error message. If the connection to the database fails, the error message is as follows:

Failed to connect to the database: Access denied for user 'root'@'localhost' (using password: YES)

After the PHP program is executed, the connection to the database will be automatically closed. If you want to close the database connection before execution is completed, you can use the mysql_close() function:

mysql_close( $conn );

Please note that the parameters are the corresponding connection resource variables.

Manage MySQL database

MySQL management is usually done through phpMyAdmin. For the installation of phpMyAdmin, please refer to "phpMyAdmin Installation".

After logging in to phpMyAdmin, you can perform various management operations allowed within the account permissions. phpMyAdmin adopts an HTML framework structure. The left side is the library table selection, and the right side is the main operation interface.

phpmyadmin main interface

On the main operation interface on the right, you can "browse data", "modify structure", "run SQL statements", "search", "insert data", "export" and "import" on the database table ", "table attribute change", "clear" or even "delete" and other operations. There will be some slight differences in the interface and operation of different versions.

Before managing a database, you need to understand some basic concepts such as creating libraries, permissions, tables and table attributes, and data types. To learn this knowledge please continue with the following tutorials.

Tips

Hosts that usually provide database support will also provide phpMyAdmin to facilitate users to manage their own databases.


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template