Taking the mysql_query() function as the basic premise of the tutorial, let’s first take a look at the usage of mysql_query():
mysql_query() function
In the PHP MySQL function library, the mysql_query() function is used to send and execute SQL statements to MySQL.
For SQL that does not return a result set with data, such as UPDATE, DELETE, etc., it returns TRUE when executed successfully and FALSE when an error occurs; for SELECT, SHOW, EXPLAIN or DESCRIBE statements, a resource identifier is returned, and FALSE is returned if the query is executed incorrectly. .
Syntax:
resource mysql_query( string query [, resource connection] )
Parameter description:
Tips
If there is no open connection, this function will try to call the mysql_connect() function without parameters to establish a connection
For queries that return a data set, even if the return result is 0 (That is, there are no records that meet the query conditions), the resource identifier is still returned instead of FALSE
Example 1:
<php $conn = @mysql_connect("localhost","root","root123"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } mysql_select_db("test", $conn); $result = mysql_query("SELECT * WHERE 1=1") or die("无效查询: " . mysql_error()); ?> 该例子查询语句在 SQL 语法上有错误,因此 mysql_query() 执行失败并返回 FALSE 。 例子2: <php $conn = @mysql_connect("localhost","root","root123"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } mysql_select_db("test", $conn); mysql_query("set names 'gbk'"); //为避免中文乱码做入库编码转换 $password = md5("123456"); //原始密码 12345 经过加密后得到加密后密码 $regdate = time(); //得到时间戳 $sql = "INSERT INTO user(username, password, email, regdate)VALUES('小王', '$password', '12345@163.com', $regdate)"; if(!mysql_query($sql,$conn)){ echo "添加数据失败:".mysql_error(); } else { echo "添加数据成功!"; } ?>
This example writes data to the user table and returns TRUE successfully, otherwise it returns FALSE (use the ! symbol to judge).
Create Database Create a database
Create a database
CREATE DATABASE syntax is used to create a database.
Syntax:
CREATE DATABASE db_name
In the PHP MySQL function library, the mysql_query() function is used to send and execute SQL statements to MySQL.
Create a database named testdb:
<?php $conn = @mysql_connect("localhost","root","root1234"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } if (@mysql_query("CREATE DATABASE testdb",$conn)){ echo "创建数据库成功!"; } else { echo "创建数据库失败:" . mysql_error(); } ?>
Tips
Creating a database requires corresponding user permissions, such as root user
In the actual virtual host space, the virtual host provider has usually created the corresponding database, so the above example It may not run successfully
Select a database
When you want to perform operations on a database or table, you need to select a database. mysql_select_db() is used to select a database. The function returns true if successful and false if failed.
Syntax:
bool mysql_select_db( string db_name [, resource connection] )
Parameter description:
For specific usage, see the example of creating a data table below.
Create a data table
Create a data table The SQL syntax is as follows:
CREATE TABLE table_name ( column1 data_type, column2 data_type, column3 data_type, ....... )
In the above syntax, column is the field name, followed by the data type.
Create a table named user:
<?php $conn = @mysql_connect("localhost","root","root1234"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } //选择数据库 mysql_select_db("test", $conn); //创建数据表 SQL $sql = "CREATE TABLE user ( uid mediumint(8), username varchar(20), password char(32), email varchar(40), regdate int(10) )"; if(!mysql_query($sql,$conn)){ echo "创建数据表失败:". mysql_error(); } else { echo "创建数据表成功!"; } ?>
In this example, it is divided into 3 execution steps:
Create a database link
Use the mysql_select_db() function to select the database that accommodates the table
Use the mysql_query() function to create a data table
In this example, the table created has 4 fields and the corresponding data object type is specified.
Principles of table building
Generally speaking, there are the following precautions when creating a data table:
The corresponding relationship between the original record data and the table
The table name and field name should follow the naming syntax and the meaning should be clear
Specify the data type of the field
Specify the field's Other attributes such as whether it is non-empty, whether there is a default value, etc.
Define table attributes such as primary and foreign keys, constraints, indexes, etc.
The relationship with other tables
Due to space limitations and to control the difficulty of the tutorial, we will not discuss too much here .
Tips
This table creation example is only to demonstrate the basic table creation syntax and is not complete. In actual production, we also need to specify more attributes for tables and fields.
For more tutorials and related articles on using PHP’s mysql_query() function to create a MySQL database, please pay attention to the PHP Chinese website!