Home >Backend Development >PHP Problem >How to open php database

How to open php database

尚
Original
2019-10-28 11:24:194722browse

How to open php database

PHP database operation steps

Recommendation:php server

1. Connect to the database server

mysql_connect('主机名[:端口号]','用户名','密码');
//例如 mysql_connect('localhost','root','root');

The resource type is returned when successful, and false when failed.

How to open php database2. Set the character set

发送执行SQL语句的语法:
$sql='';
mysql_query($sql);
例如设置字符集:
$sql='set names utf8';
mysql_query($sql);

The default character set of the database server is utf8, and the encoding character set of PHP is utf8.

3. Select the database

To operate the data, you need to specify the database.

mysql_query("use db_name");

If the SQL statement fails to execute, no error message will be prompted and can only be obtained through the mysql_error function.

4. Close the connection

mysql_close(resource [$link]);

Close the database connection and release the memory. Connections that are not closed are automatically destroyed at the end of script execution.

How to open php database

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