Mysql(i) support has been added since php5.0, and the new functions are added in the form of objects
i means improvement, multiple functions, high efficiency and stability
Compile time parameters:
./configure --with-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=/usr/bin/mysql_config \ <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">使用 Mysql ClientLibrary(libmysql)构建 --with-mysqli=mysqlnd \ <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">使用 Mysql Native Dirver 即mysqlnd --with-pdo-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=mysqlnd <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">使用 Mysql Native Dirver 即mysqlnd </span></span></span></span></span></span></span></span>
Due to copyright issues, starting from php5.3, php starts to use mysqlnd instead of libmysql.dll
mysqlnd is a mysql database driver developed by zend. It has improved in all aspects compared to the original one
#Compile using mysqlnd
./configure --with-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=mysqlnd 加上你的参数 </span></span>
mysqli process and object methods are supported
Three classes provided by mysqli:
1. Mysqli and connection related
2. MySQLi_Result handles result sets
3. mysqli_stmt preprocessing class
#Set character set
set_charset
#Get character set
character_set_name
Get database object
<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 1 //屏蔽连接产生的错误 <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: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 2 可以设置一些参数 <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></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
query: Returns false if it fails, select returns the result set object if it succeeds, others return true instead of false, which means that the sql execution is successful
No result set example
<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 = "insert into limove(`name`, `order`) values('aa', 11)"<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->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$sql = "delete from limove where id = 221"<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->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: #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;">$mysqli-><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;">插入的id ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">insert_id); ee(<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>
There is a result set
<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 = "select * from limove as limove_as"<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="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: #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;">$result-><span style="font-family: 'Courier new' !important;">num_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;">$result-><span style="font-family: 'Courier new' !important;">field_count); <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;">$result-><span style="font-family: 'Courier new' !important;">field_count); <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;">$field_arr = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_fields(); <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">移动字段的指针 // $result->field_seek(1); <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;">while(<span style="color: #800080; font-family: 'Courier new' !important;">$field = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_field()) { ee(<span style="color: #800080; font-family: 'Courier new' !important;">$field<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;">$result->data_seek(1<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;">$data = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_all(MYSQLI_ASSOC); <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: #0000ff; font-family: 'Courier new' !important;">array<span style="font-family: 'Courier new' !important;">(); <span style="color: #800080; font-family: 'Courier new' !important;">$result->data_seek(0); <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;">while(<span style="color: #800080; font-family: 'Courier new' !important;">$row = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_assoc()) { <span style="color: #800080; font-family: 'Courier new' !important;">$data[] = <span style="color: #800080; font-family: 'Courier new' !important;">$row<span style="font-family: 'Courier new' !important;">; } ee(<span style="color: #800080; font-family: 'Courier new' !important;">$data<span style="font-family: 'Courier new' !important;">); <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">free(); <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>
一次 执行多条语句 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>