mysqli_info()函数返回最近执行的 SQL 语句的信息
mysqli_info()函数返回有关最近MySQLi函数调用执行的查询的信息。此函数支持仅以下格式的查询:
INSERT INTO...SELECT....
INSERT INTO...VALUES (...),(...),(...).
LOAD DATA INFILE ....
ALTER TABLE ....
UPDATE ....
mysqli_info($con)
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
PHP mysqli_info()函数返回一个字符串值,该字符串值表示最近执行的查询的描述/信息。如果执行的最新查询不是受支持的查询,则此函数返回一个空字符串。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_info()函数的用法(面向过程风格)-
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //查询并将记录插入到employee表中 mysqli_query($con, "INSERT INTO employee VALUES ('Sarmista', 'Sharma', 28, 'F', 15000, 101), ('Sheldon', 'Cooper', 25, 'M', 2256, 102)"); //查询信息 $error = mysqli_info($con); print("查询信息: ".$error); //关闭连接 mysqli_close($con); ?>
输出结果
查询信息: Records: 2 Duplicates: 0 Warnings: 0
在面向对象的样式中,此函数的语法为$ con-> info。以下是面向对象样式的此函数的示例-
<?php //建立连接 $con = new mysqli("localhost", "root", "password", "mydb"); //Query to retrieve all the rows of employee table $con -> query("INSERT INTO employee VALUES ('Sarmista', 'Sharma', 28, 'F', 15000, 101), ('Sheldon', 'Cooper', 25, 'M', 2256, 102)"); //查询信息 $info = $con ->info; print("查询信息: ".$info); //关闭连接 $con -> close(); ?>
输出结果
查询信息: Records: 2 Duplicates: 0 Warnings: 0
以下是mysqli_info()函数的另一个示例-
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //ALTER TABLE 查询 mysqli_query($con, "ALTER TABLE table_name DROP COLUMN CONTACT"); print("Info: ".mysqli_info($con)."\n"); //UPDATE 查询 mysqli_query($con, "UPDATE employee set INCOME=INCOME+5000"); print("Info: ".mysqli_info($con)."\n"); //INSERT 查询 mysqli_query($con, "INSERT INTO employee (FIRST_NAME, AGE) VALUES (Archana, 25), (Bhuvan, 29)"); print("Info: ".mysqli_info($con)."\n"); //INSERT 插入使用 SELECT语句 mysqli_query($con, "INSERT into employee(FIRST_NAME, LAST_NAME, AGE) select 'Manoj', 'Tiwari', 45"); print("Info: ".mysqli_info($con)."\n"); //关闭连接 mysqli_close($con); ?>
输出结果
Info: Info: Rows matched: 3 Changed: 3 Warnings: 0 Info: Rows matched: 3 Changed: 3 Warnings: 0 Info: Records: 1 Duplicates: 0 Warnings: 0
返回最近执行查询的信息:
<?php $connection_mysql = mysqli_connect("localhost","root","password","mydb"); if (mysqli_connect_errno($connection_mysql)){ echo "连接MySql失败: " . mysqli_connect_error(); } $sql1 = "CREATE TABLE NewTable SELECT * FROM(employee)"; mysqli_query($connection_mysql,$sql1); echo mysqli_info($connection_mysql); mysqli_close($connection_mysql); ?>
输出结果
Records: 7 Duplicates: 0 Warnings: 0