阮一峰的IT笔记:首页 -> 分类 -> 数据库 查看所有文章:按分类 | 按月份

MySQLi函数库

1.

版本支持:

PHP > 5.0

MySQL > 4.0

2.

连接数据库:

$db = new mysqli(host,database,user,password);

这是对象导向的连接方法,还有一种过程导向的连接方法。前者返回一个对象,后者返回一个资源。

$db = mysqli_connect(host,database,user,password);

3.

连接出错函数:

mysqli_connect_error();

如果连接出错,该函数返回一个错误号,否则返回0;

4.

选择数据库:

$db->select_db(dbname);

or

mysqli_select_db(db_resource,db_name);

5.

执行查询

$result = $db->query($query);

or

$result = mysqli_query($db,$query);

6.

返回结果的行数:

$num_results = $result->num_rows;

or

$num_results = mysqli_num_rows($result);

7.

取出每一行的结果(返回关系型数组):

$row = $result->fetch_assoc();

or

$row = mysqli_fetch_assoc($result);

8.

取出每一行的结果(返回计数型数组):

$row = $result->fetch_row($result);

or

$row = mysqli_fetch_row($result);

9.

取出每一行的结果(返回一个对象):

$row = $result->fetch_object();

or

$row = mysqli_fetch_object($result);

10.

释放查询结果:

$result->free();

or

mysqli_free_result($result);

11.

关闭数据库连接:

$db->close();

or

mysqli_close($db);

12.

一条查询所影响的行数:

$db->affected_rows;

or

mysqli_affected_rows($result);

13.

模式化SQL语句执行:

$query = “insert into books values(?, ?, ?, ?)”;
$stmt = $db->prepare($query);
$stmt->bind_param(“sssd”, $isbn, $author, $title, $price);
$stmt->execute();
echo $stmt->affected_rows.’ book inserted into database.’;
$stmt->close();

在过程化执行方式中,

$db->prepare()对应mysqli_stmt_prepare()函数;
$stmt->bind_param()对应mysqli_stmt_bind_param()函数;
$stmt->execute() 对应mysqli_stmt_execute()函数;

« MySQL中的子查询(subqueries) | 首页 | 一个PHP错误处理范例 »

About

This page contains a single entry from the blog posted on 2007年11月14日 12:16.

The previous post in this blog was MySQL中的子查询(subqueries).

The next post in this blog is 一个PHP错误处理范例.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment