本文只针对 MySQLi 面向过程的函数使用方法。
1 数据库连接相关函数
mysqli_connect(host, user, pwd, db, port, socket), 全部参数都是可选的,默认会从php.ini文件中找对应的值。
$con = mysqli_connect($servername, $username, $pwd, $db);
if (mysqli_connect_errno()) // 返回连接错误的代码,没错误返回0
die(mysqli_connect_error()); // 返回连接错误的字符串,没错误返回null
...
mysqli_close($con);
2 SQL 常用函数
mysqli_insert_id($con), 返回最后一次查询所生成ID。
mysqli_affected_rows(con), 返回上一次insert/update/replace/delete/select影响的行数
mysqli_query($con, $sql [,resultatmode]), 返回结果集。
2a 调试
msyqli_errno(con), 返回最近用的函数的错误代码
mysqli_error(con), 返回最近用的函数的错误描述字符串
mysqli_error_list(con), 返回最近用的函数的错误列表(错误代码,错误文本,sqlistate的管理数组)
mysqli_info(con), 返回上一次查询的额外信息 string
mysqli_sqlstate(con), 返回最后一个SQLSTATE错误代码,‘00000’表示没有错误
mysqli_warning_count(con), 返回上一次查询的警告次数
2b 多个查询
mysqli_multi_query($con, $sql), 执行多个查询。
mysqli_next_result($con), 下一个结果集。
mysqli_more_result($con), 检查批量查询中是否还有查询结果。
mysqli_store_result(), 转移上次查询返回的结果集。
mysqli_user_result(),
3 处理结果集的函数
3a 行:
mysqli_data_seek(res, offset), 移动行指针,返回bool
mysqli_fetch_all(res, [type]), 返回结果集的所有行组成的数组,type索引或关联或两者
mysqli_fetch_array(res, [type]), 返回结果集中的当前行,type同上
mysqli_fetch_assoc(res), 返回结果集中的当前行,关联数组
mysqli_fetch_row(res), 返回结果集中的当前行,索引数组
mysqli_fetch_object(res), 返回结果集中的当前行,对象
mysqli_fetch_lengths(res), 返回当前行的每个字段长度组成的数组。
mysqli_num_rows(res), 结果集的行数。
3b 列:
mysqli_num_fields(res), 结果集的列数
mysqli_field_count(con), 当前结果集列的数量,值应该同上
mysqli_field_seek(res, offset), 移动列指针
mysqli_field_tell(res), 返回当前列指针位置
mysqli_fetch_fields(res), 返回结果集的所有列(字段)定义信息组成的数组,每个列定义信息是一个对象。
mysqli_fetch_field(res), 返回结果集中当前列定义信息,对象
mysqli_fetch_field_direct(res, n), 直接返回第n列定义信息,对象
3c 其它结果集函数
mysqli_fres_result(res), 释放结果集内存。
4 预处理相关函数
减少了分析时间,只做一次查询(尽管语句多次执行)
减少了服务器宽带,只需要发送查询的参数,而不是整个语句
防止sql注入,因为参数值发送或使用的协议,保证了数据的合法性
4a 预处理插入数据库
$sql = "insert into MyGuests (firstname, lastname, email)
values(?, ?, ?)";
$stmt = mysqli_stmt_init($con); // 初始化 statement对象
if (mysqli_stmt_prepare($stmt, $sql)) { // 预处理
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
$firstname = 'john';
$lastname ='Doe';
$email = 'john@gmail.com';
mysqli_stmt_execute($stmt);
$firstname = 'Mary';
$lastname ='Moe';
$email = 'mary@gmail.com';
mysqli_stmt_execute($stmt);
$firstname = 'Julie';
$lastname ='Dooley';
$email = 'julie@gmail.com';
mysqli_stmt_execute($stmt);
} else {
echo 'something wrong!';
echo PHP_EOL;
}
mysqli_close($con);
4b 预处理处理结果集,方法一
$stmt = mysqli_stmt_init($con);
$sql = "select * from MyGuests where id = ?";
if (mysqli_stmt_prepare($stmt, $sql)) {
$id = 3;
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $col1, $col2, $col3, $col4, $col5);
mysqli_stmt_fetch($stmt);
for ($i=1;$i<6;$i++) {
print_r(${'col'.$i});
echo PHP_EOL;
}
} else {
echo 'prepare doesn\'t work!';
echo PHP_EOL;
}
4c 预处理处理结果集,方法二
$sql = "select * from MyGuests where id = ?";
if ($stmt=mysqli_prepare($con, $sql)) {
mysqli_stmt_bind_param($stmt, 'i', $id);
$id =3;
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $col1, $col2, $col3, $col4, $col5);
mysqli_stmt_fetch($stmt);
for ($i=1;$i<6;$i++) {
print_r(${'col'.$i});
echo PHP_EOL;
}
} else {
echo 'prepare doesn\'t work!';
echo PHP_EOL;
}
5 事务处理
$sql1 = '。。。';
$sql2 = '。。。';
mysqli_autocommit($con, false);
if (mysqli_query($con, $sql1) && mysqli_query($con, $sql2)) {
mysqli_commit($con);
echo 'ok';
} else {
mysqli_rollback($con);
echo 'not ok';
}
echo PHP_EOL;
mysqli_autocommit(true);