Php/func mysqli query
来自菜鸟教程
PHP mysqli query()函数
示例-面向对象的样式
对数据库执行查询:
<?php $mysqli = new mysqli("localhost","my_user","my_password","my_db"); // Check connection if ($mysqli -> connect_errno) { echo "Failed to connect to MySQL: " . $mysqli -> connect_error; exit(); } // Perform query if ($result = $mysqli -> query("SELECT * FROM Persons")) { echo "Returned rows are: " . $result -> num_rows; // Free result set $result -> free_result(); } $mysqli -> close(); ?>
在底部查看程序样式的示例。
定义和用法
query()/ mysqli_query()函数对数据库执行查询。
句法
面向对象的样式:
$mysqli -> query(query, resultmode)
程序风格:
mysqli_query(connection, query, resultmode)
参数值
参数 | 描述 |
---|---|
connection | 需要。指定要使用的MySQL连接 |
query | 需要。指定SQL查询字符串 |
resultmode |
可选的。一个常数。可以是以下之一:
|
技术细节
返回值: | 对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,它将返回mysqli_result对象。对于其他成功的查询,它将返回TRUE。失败时为假 |
PHP版本: | 5+ |
PHP更新日志: | PHP 5.3.0添加了异步查询功能 |
示例-程序风格
对数据库执行查询:
<?php $con = mysqli_connect("localhost","my_user","my_password","my_db"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } // Perform query if ($result = mysqli_query($con, "SELECT * FROM Persons")) { echo "Returned rows are: " . mysqli_num_rows($result); // Free result set mysqli_free_result($result); } mysqli_close($con); ?>