Home >Backend Development >PHP Problem >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.
2. 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.
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!