Mysql(i) サポートは php5.0 から追加され、新しい関数はオブジェクトの形式で追加されます
i は改善、多機能、高効率、安定性を意味します
コンパイル時パラメータ:
著作権の問題により、php5.3 以降、php は libmysql.dll の代わりに mysqlnd を使用し始めます
mysqlnd は、zend によって開発された mysql データベース ドライバーです。元のドライバーと比較してあらゆる面で改良されています
#mysqlnd を使用してコンパイルする
mysqliプロセスとオブジェクトメソッドがサポートされています
mysqli が提供する 3 つのクラス:
1. Mysqli と接続関連
2. MySQLi_Result は結果セットを処理します
3. mysqli_stmt 前処理クラス
#文字セットを設定する
set_charset
#キャラクターセットを取得する
キャラクターセット名
データベースオブジェクトを取得する
クエリ: 失敗した場合は false を返し、select は成功した場合は結果セット オブジェクトを返します。その他は true を返しますが false は返しません。これは、SQL の実行が成功したことを意味します
結果セットの例はありません
結果セットがあります
一次 执行多条语句 multiquery (不推荐使用)
无结果集,此时 affected_rows 只能获取到最后的那条影响的条数
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_init<span style="font-family: 'Courier new' !important;">(); <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2);<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">设置超时时间 <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->real_connect('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr = <span style="color: #0000ff; font-family: 'Courier new' !important;">array<span style="font-family: 'Courier new' !important;">( 'insert into limove(id,`name`, `order`) values(null, 1, 2)', 'insert into limove(id,`name`, `order`) values(null, 1, 222)', 'delete from limove where `order` = 2',<span style="font-family: 'Courier new' !important;"> ); <span style="color: #800080; font-family: 'Courier new' !important;">$sql = <span style="color: #008080; font-family: 'Courier new' !important;">implode(';', <span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$result = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->multi_query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">); <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$result === <span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">) { ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">errno); ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">error); } <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->close(); </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
有结果集
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_init<span style="font-family: 'Courier new' !important;">(); <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2);<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">设置超时时间 <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->real_connect('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr = <span style="color: #0000ff; font-family: 'Courier new' !important;">array<span style="font-family: 'Courier new' !important;">( 'show tables', 'desc select * from limove', 'show create table limove',<span style="font-family: 'Courier new' !important;"> ); <span style="color: #800080; font-family: 'Courier new' !important;">$sql = <span style="color: #008080; font-family: 'Courier new' !important;">implode(';', <span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$rst = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->multi_query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">); <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$rst === <span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">) { ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">errno); ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">error); } <span style="color: #0000ff; font-family: 'Courier new' !important;">do<span style="font-family: 'Courier new' !important;">{ <span style="color: #800080; font-family: 'Courier new' !important;">$result = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->store_result();<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">获取当前光标所在的结果集 <span style="color: #800080; font-family: 'Courier new' !important;">$data = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_all(); ee(<span style="color: #800080; font-family: 'Courier new' !important;">$data<span style="font-family: 'Courier new' !important;">); }<span style="color: #0000ff; font-family: 'Courier new' !important;">while(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->next_result());<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">光标移动到下一个结果集 <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->close(); </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
事务处理:
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli=<span style="color: #0000ff; font-family: 'Courier new' !important;">new mysqli("localhost", "root", "123456", "xsphpdb"<span style="font-family: 'Courier new' !important;">); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">事务处理 <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->autocommit(0<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">true<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$price=50<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$sql="update zh set ye=ye-{<span style="color: #800080; font-family: 'Courier new' !important;">$price} where name='zhangsan'"<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$result=<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">); <span style="color: #0000ff; font-family: 'Courier new' !important;">if(!<span style="color: #800080; font-family: 'Courier new' !important;">$result<span style="font-family: 'Courier new' !important;">){ <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">; <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "从张三转出失败<br> "<span style="font-family: 'Courier new' !important;">; }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{ <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->affected_rows==0<span style="font-family: 'Courier new' !important;">){ <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">; <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "张三的钱没有变化"<span style="font-family: 'Courier new' !important;">; }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{ <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "从张三账号中转出成功!<br> "<span style="font-family: 'Courier new' !important;">; } } <span style="color: #800080; font-family: 'Courier new' !important;">$sql="update zh set ye=ye+{<span style="color: #800080; font-family: 'Courier new' !important;">$price} where name='lisi1'"<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$result=<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">); <span style="color: #0000ff; font-family: 'Courier new' !important;">if(!<span style="color: #800080; font-family: 'Courier new' !important;">$result<span style="font-family: 'Courier new' !important;">){ <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">; <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "从李四转入失败<br> "<span style="font-family: 'Courier new' !important;">; }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{ <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->affected_rows==0<span style="font-family: 'Courier new' !important;">){ <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">; <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "李四的钱没有变化"<span style="font-family: 'Courier new' !important;">; }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{ <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "向李四账号中转入成功!<br> "<span style="font-family: 'Courier new' !important;">; } } <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$error<span style="font-family: 'Courier new' !important;">){ <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "转账成功!"<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">commit(); }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{ <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "转账失败!"<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">rollback(); } <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->autocommit(1<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->close(); </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
mysqli_stmt:mysqli预处理类(推荐):表示了准备好的一个语句,服务器端只编译一次sql
用mysqli和mysqli_result可以实现同样的功能
优点:效率高,适用于语句相同只是数据不同的情况 ,可以阻止sql注入的产生
mysqli_stmt示例:非select语句
<span style="color: #0000ff; font-family: 'Courier new' !important;">require 'fns.php'<span style="font-family: 'Courier new' !important;">; <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = @<span style="color: #0000ff; font-family: 'Courier new' !important;">new mysqli('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">只能用函数来判断是否连接成功 <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_errno<span style="font-family: 'Courier new' !important;">()) { <span style="color: #0000ff; font-family: 'Courier new' !important;">echo <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_error<span style="font-family: 'Courier new' !important;">(); <span style="color: #0000ff; font-family: 'Courier new' !important;">die<span style="font-family: 'Courier new' !important;">; } <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->set_charset('utf8'<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$sql = "insert into limove values(?, ?, ?)"; <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">语句一样值不相同情况 <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">mysqli中有直接的方法可用 <span style="color: #800080; font-family: 'Courier new' !important;">$stmt = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->prepare(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">绑定参数 <span style="color: #800080; font-family: 'Courier new' !important;">$stmt->bind_param('iss', <span style="color: #800080; font-family: 'Courier new' !important;">$id, <span style="color: #800080; font-family: 'Courier new' !important;">$name, <span style="color: #800080; font-family: 'Courier new' !important;">$order<span style="font-family: 'Courier new' !important;">); <span style="color: #0000ff; font-family: 'Courier new' !important;">for(<span style="color: #800080; font-family: 'Courier new' !important;">$i=0;<span style="color: #800080; font-family: 'Courier new' !important;">$i$i++<span style="font-family: 'Courier new' !important;">){ <span style="color: #800080; font-family: 'Courier new' !important;">$id = 0<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$name = 'name'<span style="font-family: 'Courier new' !important;">; <span style="color: #800080; font-family: 'Courier new' !important;">$order = <span style="color: #008080; font-family: 'Courier new' !important;">mt_rand(1, 1000<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">execute(); } <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">最后id ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">insert_id); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">影响的行数 注:最后一条执行的 ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">affected_rows); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">错误号 ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">errno); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">错误信息 ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">error); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">stmt对象中可以看到更多的信息 ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt<span style="font-family: 'Courier new' !important;">); eee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli); </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
mysqli_stmt示例:select语句 1
<span style="color: #0000ff; font-family: 'Courier new' !important;">require 'fns.php'<span style="font-family: 'Courier new' !important;">; <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = @<span style="color: #0000ff; font-family: 'Courier new' !important;">new mysqli('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">); <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">只能用函数来判断是否连接成功 <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_errno<span style="font-family: 'Courier new' !important;">()) { <span style="color: #0000ff; font-family: 'Courier new' !important;">echo <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_error<span style="font-family: 'Courier new' !important;">(); <span style="color: #0000ff; font-family: 'Courier new' !important;">die<span style="font-family: 'Courier new' !important;">; } <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->set_charset('utf8'<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$sql = "select * from limove where id </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>