English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La fonction mysqli_query() exécute une requête sur la base de données
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()用法(procedural style)-
<?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("Create table ..."."\n"); //Insert the record 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', 'Afrique du Sud')"); 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("insert record ..."."\n"); //Fermer la connexion mysqli_close($con); ?>
Résultat de la sortie
Création de la table... insert record ...
If you observe the content of the table in the database, you can see the inserted records as follows:
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)
In the object-oriented style, the syntax of this function is}}$con-> query();。The following is an example of this function in an object-oriented style;
<?php $con = new mysqli("localhost", "root", "password", "mydb"); //Insert the record 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("Create data......"); //Fermer la connexion $res = $con -> close(); ?>
Résultat de la sortie
Create data......
If you observe the content of the table in the database, you can see the inserted records as follows:
mysql> select * from players; +------------+-----------+-------------+ | First_Name | Last_Name | Country | +------------+-----------+-------------+ | Shikhar | Dhawan | India | | Jonathan | Trott | SouthAfrica | +------------+-----------+-------------+ 2 rows in set (0.00 sec)
The following example prints the results of INSERT and SELECT queries-
<?php //Établir une connexion $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("Create table ..."."\n"); //Insert the record into the my_team table $res = mysqli_query($con, "insert into my_team values("1, 'Shikhar', 'Dhawan', 'Delhi', 'India")"); print("insert query result: ", $res, "\n"); $res = mysqli_query($con, "insert into my_team values("2, 'Jonathan', 'Trott', 'CapeTown', 'Afrique du Sud')"); print("Résultat de la requête d'insertion : ").$res; $res = mysqli_query($con, "SELECT * FROM my_team"); print("Résultat de la requête SELECT : "); print_r($res); //Fermer la connexion mysqli_close($con); ?>
Résultat de la sortie
Création de la table... Résultat de la requête d'insertion : 1 Résultat de la requête d'insertion : 1Résultat de la requête SELECT : Objet mysqli_result ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 2 [type] => 0 )
Supposons que nous ayons déjà créé une table players dans la base de données et qu'elle soit remplie comme suit-
CREATE TABLE Players (Name VARCHAR(255), Age INT, Score INT); insert into Players values('Dhavan', 33, 9),('Rohit', 28, 26),('Kohli', 25, 50);
Le fichier suivant est un exemple d'exécution de requêtes sur une base de données :
<?php //Établir une connexion $con = mysqli_connect("localhost", "root", "password", "mydb"); //Exécuter plusieurs requêtes $query = "SELECT * FROM players"; //Recherche d'enregistrements $res = mysqli_query($con, $query, MYSQLI_USE_RESULT); if ($res) { while ($row = mysqli_fetch_row($res)) { print("Nom : ".$row[0]."\n"); print("Âge : ".$row[1]."\n"); } } //Fermer la connexion mysqli_close($con); ?>
Résultat de la sortie
Nom : Dhavan Âge : 33 Nom : Rohit Âge : 28 Nom : Kohli Âge : 25