Maison > php教程 > php手册 > le corps du texte

php操纵mysqli数据库

WBOY
Libérer: 2016-09-19 08:55:12
original
889 Les gens l'ont consulté

从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加

i表示改进的意思 功能多、效率高、稳定

编译时参数:

./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>
Copier après la connexion

由于版权问题 从 php5.3开始 php开始用 mysqlnd 替代 libmysql.dll 
mysqlnd 是zend公司开发的mysql数据库驱动,相比原来各方面都有所提高

#使用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>
Copier après la connexion

mysqli 过程、对象方式都支持

mysqli提供的三个类:
   1、mysqli 和连接相关的
   2、MySQLi_Result 处理结果集
   3、mysqli_stmt 预处理类

#设置字符集
set_charset

#获取字符集
character_set_name

获取数据库对象

复制代码
<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>
Copier après la connexion
复制代码

query:失败返回false,select成功返回结果集对象,其他返回true 非false,意味着sql执行成功了

无结果集示例

复制代码
<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>
Copier après la connexion
复制代码

 

有结果集

 

复制代码
<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>
Copier après la connexion
复制代码

一次 执行多条语句 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>
Copier après la connexion
复制代码

 

有结果集

复制代码
<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>
Copier après la connexion
复制代码

 

事务处理:

复制代码
    <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>
Copier après la connexion
复制代码

 

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>
Copier après la connexion
复制代码

 

  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>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal