mysqli_query()函数对数据库执行一次查询
mysqli_query()函数接受表示查询的字符串值作为参数之一,并在数据库上执行给定的查询。
mysqli_query($con, query)
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
2 | query(必需) 这是一个字符串值,表示要执行的查询。 |
3 | mode(可选) 这是表示结果模式的整数值。您可以将MYSQLI_USE_RESULT或MYSQLI_STORE_RESULT作为值传递给此参数。 |
失败时返回 FALSE,通过mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回一个mysqli_result 对象,其他查询则返回TRUE。
对于其他查询此函数返回一个布尔值,如果操作/查询成功,则为true,否则为false。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_query()函数的用法(面向过程风格)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("创建表 ..."."\n"); //将记录插入到my_team表中 mysqli_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')"); print("插入记录 ..."."\n"); //关闭连接 mysqli_close($con); ?>
输出结果
创建表 ... 插入记录 ...
如果您观察数据库中表的内容,则可以看到插入的记录,如下所示:
mysql> select * from my_team; +------+------------+------------+----------------+-------------+ | ID | First_Name | Last_Name | Place_Of_Birth | Country | +------+------------+------------+----------------+-------------+ | 1 | Shikhar | Dhawan | Delhi | India | | 2 | Jonathan | Trott | CapeTown | SouthAfrica | | 3 | Kumara | Sangakkara | Matale | Srilanka | | 4 | Virat | Kohli | Delhi | India | +------+------------+------------+----------------+-------------+ 4 rows in set (0.00 sec)
在面向对象的样式中,此函数的语法为$con->query();。以下是面向对象风格中此函数的示例;
<?php $con = new mysqli("localhost", "root", "password", "mydb"); //将记录插入到players表中 $con->query("CREATE TABLE IF NOT EXISTS players(First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))"); $con->query("insert into players values('Shikhar', 'Dhawan', 'India')"); $con->query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')"); print("创建数据......"); //关闭连接 $res = $con -> close(); ?>
输出结果
创建数据......
如果您观察数据库中表的内容,则可以看到插入的记录,如下所示:
mysql> select * from players; +------------+-----------+-------------+ | First_Name | Last_Name | Country | +------------+-----------+-------------+ | Shikhar | Dhawan | India | | Jonathan | Trott | SouthAfrica | +------------+-----------+-------------+ 2 rows in set (0.00 sec)
以下示例打印INSERT和SELECT查询的结果-
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("创建表 ..."."\n"); //将记录插入到my_team表中 $res = mysqli_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')"); print("插入查询的结果: ".$res."\n"); $res = mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); print("插入查询的结果: ".$res); $res = mysqli_query($con, "SELECT * FROM my_team"); print("Result of the SELECT query: "); print_r($res); //关闭连接 mysqli_close($con); ?>
输出结果
创建表 ... 插入查询的结果: 1 插入查询的结果: 1Result of the SELECT query: mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 2 [type] => 0 )
假设我们已经在数据库中创建了一个players表并填充了它,如下所示-
CREATE TABLE Players (Name VARCHAR(255), Age INT, Score INT); insert into Players values('Dhavan', 33, 90),('Rohit', 28, 26),('Kohli', 25, 50);
以下示例执行针对数据库的查询:
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //执行多个查询 $query = "SELECT * FROM players"; //检索记录 $res = mysqli_query($con, $query, MYSQLI_USE_RESULT); if ($res) { while ($row = mysqli_fetch_row($res)) { print("Name: ".$row[0]."\n"); print("Age: ".$row[1]."\n"); } } //关闭连接 mysqli_close($con); ?>
输出结果
Name: Dhavan Age: 33 Name: Rohit Age: 28 Name: Kohli Age: 25