定義和用法
這個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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。