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

How to open php database

Release: 2023-02-27 20:54:02
Original
4717 people have browsed it

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');
Copy after login

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);
Copy after login

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");
Copy after login

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]);
Copy after login

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!

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