对数据库执行查询:
<?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)
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies the SQL query string |
resultmode | Optional. A constant. Can be one of the following:
|
返回值: | 对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,它将返回 mysqli_result 对象。对于其他成功的查询,它将返回 TRUE。失败时为 FALSE |
---|---|
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);
?>
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!