定义和用法
这个mysqli_query()函数接受一个表示查询的字符串值作为参数之一,并在数据库上执行/执行给定的查询。
用法
mysqli_query($con, query)
参数
Sr.No | 参数及说明 |
---|---|
1 |
con(Mandatory) 这是一个表示与 MySQL 服务器的连接的对象。 |
2 |
query(Mandatory) 这是一个字符串值,表示要执行的查询。 |
3 |
mode(Optional) 这是一个表示结果模式的整数值。您可以将 MYSQLI_USE_RESULT 或 MYSQLI_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();以下是面向对象样式 $minus 中此函数的示例;
<?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 )
示例
假设我们在数据库中创建了一个表玩家并填充它,如下所示 -
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 mysqli_get_server_info()用法及代码示例
- PHP mysqli_data_seek()用法及代码示例
- PHP mysqli_insert_id()用法及代码示例
- PHP mysqli_fetch_assoc()用法及代码示例
- PHP mysqli_connect_error()用法及代码示例
- PHP mysqli_fetch_all()用法及代码示例
- PHP mysqli_next_result()用法及代码示例
- PHP mysqli_stmt_affected_rows()用法及代码示例
- PHP mysqli_begin_transaction()用法及代码示例
- PHP mysqli_real_escape_string()用法及代码示例
- PHP mysqli_fetch_lengths()用法及代码示例
- PHP mysqli_character_set_name()用法及代码示例
- PHP mysqli_free_result()用法及代码示例
- PHP mysqli_fetch_row()用法及代码示例
- PHP mysqli_debug()用法及代码示例
- PHP mysqli_commit()用法及代码示例
- PHP mysqli_fetch_field()用法及代码示例
- PHP mysqli_kill()用法及代码示例
- PHP mysqli_thread_safe()用法及代码示例
- PHP mysqli_get_connection_stats()用法及代码示例
注:本文由纯净天空筛选整理自 PHP mysqli_query() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。