目录

PHP mysqli query() 函数

❮ PHP MySQLi 参考

示例 - 面向对象风格

对数据库执行查询:

<?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:

  • MYSQLI_USE_RESULT (Use this to retrieve large amount of data)
  • MYSQLI_STORE_RESULT (This is default)

技术细节

返回值: 对于成功的 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);
?>


❮ PHP MySQLi 参考