PHP mysqli_query() 函数



定义和用法

mysqli_query() 函数接受一个表示查询的字符串值作为参数之一,并在数据库上执行/执行给定的查询。

语法

mysqli_query($con, query)

参数

序号 参数及描述
1

con(必填)

这是一个表示与 MySQL 服务器连接的对象。

2

query(必填)

这是一个表示要执行的查询的字符串值。

3

mode(可选)

这是一个表示结果模式的整数值。您可以将MYSQLI_USE_RESULTMYSQLI_STORE_RESULT作为值传递给此参数。

返回值

对于 SELECT、SHOW、DESCRIBE 和 EXPLAIN 查询,此函数在成功的情况下返回一个mysqli_result对象,其中包含查询的结果,如果失败则返回 false。

对于其他查询,此函数返回一个布尔值,如果操作/查询成功则为true,否则为false

PHP 版本

此函数首次引入于 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("Table Created ..."."\n");

   //Inserting a records into the my_team table
   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("Records Inserted ..."."\n");
  
   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果:

Table Created ...
Records Inserted ...

如果您观察数据库中表的內容,您可以看到插入的记录,如下所示:

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");

   //Inserting a records into the players table
   $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("Data Created......");
   //Closing the connection
   $res = $con -> close();
?>

这将产生以下结果:

Data Created......

如果您观察数据库中表的內容,您可以看到插入的记录,如下所示:

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
   //Creating a connection
   $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("Table Created ..."."\n");

   //Inserting a records into the my_team table
   $res = mysqli_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')");
   print("Result of Insert Query: ".$res."\n");
   $res = mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   print("Result of Insert Query: ".$res);

   $res = mysqli_query($con, "SELECT * FROM my_team");
   print("Result of the SELECT query: ");
   print_r($res);

   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果:

Table Created ...
Result of Insert Query: 1
Result of Insert Query: 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);

以下示例从

mysqli_multi_query

函数中检索结果集:

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Executing the multi query
   $query = "SELECT * FROM players";
 
   //Retrieving the records
   $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");
      }
   }

   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果:

Name: Dhavan
Age: 33
Name: Rohit
Age: 28
Name: Kohli
Age: 25
php_function_reference.htm
广告