您的位置:首页 > 数据库 > MySQL

详细介绍 mysqli扩展类(一)

2016-04-20 00:00 344 查看
摘要: mysqli扩展,我们有时称之为MySQL增强扩展,可以用于使用 MySQL4.1.3或更新版本中新的高级特性。mysqli扩展在PHP 5及以后版本中包含。
mysqli扩展有一系列的优势,相对于mysql扩展的提升主要有:
• 面向对象接口
• prepared语句支持(译注:关于prepare请参阅mysql相关文档)
• 多语句执行支持
• 事务支持
• 增强的调试能力
• 嵌入式服务支持

我们应该如何使用mysqli类呢?

首先,当然建一个类啦

$mysql = new /mysqli( );

mysqli::__construct() ([ string
$host
= ini_get("mysqli.default_host") [, string
$username
= ini_get("mysqli.default_user") [, string
$passwd
= ini_get("mysqli.default_pw") [, string
$dbname
= "" [, int
$port
= ini_get("mysqli.default_port") [, string
$socket
= ini_get("mysqli.default_socket") ]]]]]] )
从定义可以看出,如果不赋予参数,他默认连接自己给定的数。在一个mysqli类中,是有以下的属性的:
MySQLi {
/* 属性 */
int $mysqli->affected_rows; 受影响的语句行数

string $mysqli->client_info; 客户端数据库的信息

int $mysqli->client_version; 客户端版本号

string $mysqli->connect_errno; 返回最近一次连接错误的代码

string $mysqli->connect_error; 返回一次连接错误语句

int $errno; 错误代号

array $mysqli->error_list; 错误的列表,每个作为一个关联数组包含errno,错误,和sqlstate

string $mysqli->error; 返回一次错误语句

int $mysqli->field_count;
string $mysqli->host_info; 本地服务器和连接状态的信息

string $mysqli->protocol_version; 所遵循协议的版本号

string $mysqli->server_info; 服务器数据库信息

int $mysqli->server_version; 服务器数据库版本

string $mysqli->info; 检索最近执行的查询的信息

mixed $mysqli->insert_id; 最后一个查询中使用的自动生成的id

string $mysqli->sqlstate; 返回从先前的MySQL SQLSTATE错误操作
int $mysqli->thread_id; 当前数据库连接的线程id号

int $mysqli->warning_count; 最近一次连接 的 警告语句数量

}

然后,定义查询语句 $query = 'select name from user where id = 8';

$mysql->query($query); //对数据库执行一次查询

mixed mysqli::query ( string
$query
[, int
$resultmode
= MYSQLI_STORE_RESULT ] )
其中参数
resultmode

Either the constant
MYSQLI_USE_RESULT
or
MYSQLI_STORE_RESULT
depending on the desired behavior. By default,
MYSQLI_STORE_RESULT
is used.

If you use
MYSQLI_USE_RESULT
all subsequent calls will return error Commands out of sync unless you call mysqli_free_result()
With
MYSQLI_ASYNC
(available with mysqlnd), it is possible to perform query asynchronously. mysqli_poll() is then used to get results from such queries.
如果失败则返回false,通过执行SELECT,SHOW,DESCRIBE或EXPLANIN查询会返回一个mysqli_result对象,其他查询则返回true。
什么是mysqli_result对象呢?
mysqli_result类代表从一个数据库查询中获取的结果集。详细见文档。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: