php连接mysqli面向过程,PHP基础知识总结:MySQLi 面向过程
本文只針對(duì) MySQLi 面向過(guò)程的函數(shù)使用方法。
1 數(shù)據(jù)庫(kù)連接相關(guān)函數(shù)
mysqli_connect(host, user, pwd, db, port, socket), 全部參數(shù)都是可選的,默認(rèn)會(huì)從php.ini文件中找對(duì)應(yīng)的值。
$con = mysqli_connect($servername, $username, $pwd, $db);
if (mysqli_connect_errno()) // 返回連接錯(cuò)誤的代碼,沒(méi)錯(cuò)誤返回0
die(mysqli_connect_error()); // 返回連接錯(cuò)誤的字符串,沒(méi)錯(cuò)誤返回null
...
mysqli_close($con);
2 SQL 常用函數(shù)
mysqli_insert_id($con), 返回最后一次查詢(xún)所生成ID。
mysqli_affected_rows(con), 返回上一次insert/update/replace/delete/select影響的行數(shù)
mysqli_query($con, $sql [,resultatmode]), 返回結(jié)果集。
2a 調(diào)試
msyqli_errno(con), 返回最近用的函數(shù)的錯(cuò)誤代碼
mysqli_error(con), 返回最近用的函數(shù)的錯(cuò)誤描述字符串
mysqli_error_list(con), 返回最近用的函數(shù)的錯(cuò)誤列表(錯(cuò)誤代碼,錯(cuò)誤文本,sqlistate的管理數(shù)組)
mysqli_info(con), 返回上一次查詢(xún)的額外信息 string
mysqli_sqlstate(con), 返回最后一個(gè)SQLSTATE錯(cuò)誤代碼,‘00000’表示沒(méi)有錯(cuò)誤
mysqli_warning_count(con), 返回上一次查詢(xún)的警告次數(shù)
2b 多個(gè)查詢(xún)
mysqli_multi_query($con, $sql), 執(zhí)行多個(gè)查詢(xún)。
mysqli_next_result($con), 下一個(gè)結(jié)果集。
mysqli_more_result($con), 檢查批量查詢(xún)中是否還有查詢(xún)結(jié)果。
mysqli_store_result(), 轉(zhuǎn)移上次查詢(xún)返回的結(jié)果集。
mysqli_user_result(),
3 處理結(jié)果集的函數(shù)
3a 行:
mysqli_data_seek(res, offset), 移動(dòng)行指針,返回bool
mysqli_fetch_all(res, [type]), 返回結(jié)果集的所有行組成的數(shù)組,type索引或關(guān)聯(lián)或兩者
mysqli_fetch_array(res, [type]), 返回結(jié)果集中的當(dāng)前行,type同上
mysqli_fetch_assoc(res), 返回結(jié)果集中的當(dāng)前行,關(guān)聯(lián)數(shù)組
mysqli_fetch_row(res), 返回結(jié)果集中的當(dāng)前行,索引數(shù)組
mysqli_fetch_object(res), 返回結(jié)果集中的當(dāng)前行,對(duì)象
mysqli_fetch_lengths(res), 返回當(dāng)前行的每個(gè)字段長(zhǎng)度組成的數(shù)組。
mysqli_num_rows(res), 結(jié)果集的行數(shù)。
3b 列:
mysqli_num_fields(res), 結(jié)果集的列數(shù)
mysqli_field_count(con), 當(dāng)前結(jié)果集列的數(shù)量,值應(yīng)該同上
mysqli_field_seek(res, offset), 移動(dòng)列指針
mysqli_field_tell(res), 返回當(dāng)前列指針位置
mysqli_fetch_fields(res), 返回結(jié)果集的所有列(字段)定義信息組成的數(shù)組,每個(gè)列定義信息是一個(gè)對(duì)象。
mysqli_fetch_field(res), 返回結(jié)果集中當(dāng)前列定義信息,對(duì)象
mysqli_fetch_field_direct(res, n), 直接返回第n列定義信息,對(duì)象
3c 其它結(jié)果集函數(shù)
mysqli_fres_result(res), 釋放結(jié)果集內(nèi)存。
4 預(yù)處理相關(guān)函數(shù)
減少了分析時(shí)間,只做一次查詢(xún)(盡管語(yǔ)句多次執(zhí)行)
減少了服務(wù)器寬帶,只需要發(fā)送查詢(xún)的參數(shù),而不是整個(gè)語(yǔ)句
防止sql注入,因?yàn)閰?shù)值發(fā)送或使用的協(xié)議,保證了數(shù)據(jù)的合法性
4a 預(yù)處理插入數(shù)據(jù)庫(kù)
$sql = "insert into MyGuests (firstname, lastname, email)
values(?, ?, ?)";
$stmt = mysqli_stmt_init($con); // 初始化 statement對(duì)象
if (mysqli_stmt_prepare($stmt, $sql)) { // 預(yù)處理
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 預(yù)處理處理結(jié)果集,方法一
$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 預(yù)處理處理結(jié)果集,方法二
$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 事務(wù)處理
$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);
總結(jié)
以上是生活随笔為你收集整理的php连接mysqli面向过程,PHP基础知识总结:MySQLi 面向过程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: matlab 显示多幅图像,运用matl
- 下一篇: python跳转和创建目录,Python