php 操作 mysql 预处理(未完待续)_PHP教程

WBOY
发布: 2016-07-20 11:14:31
原创
946 人浏览过

<span 由于本地 PHP 版本过底,没办法进行完整的测试<br /><br />$host</span> = 'localhost'<span ;
</span><span $user</span> = 'root'<span ;
</span><span $password</span> = ''<span ;
</span><span $dbname</span> = 'cmstop'<span ;
</span><span $mysqli</span> = <span new</span> mysqli(<span $host</span>, <span $user</span>, <span $password</span>, <span $dbname</span>) or <span exit</span>('无法连接到mysql:'.<span $mysqli</span>-><span connect_error);

 </span><span $sql</span>  = "SELECT CURRENT_USER(); SELECT * FROM `db_setting` LIMIT 5;"<span ;

</span><span if</span> (<span $mysqli</span>->multi_query(<span $sql</span>)){<span //</span><span 多条查询</span>
    <span do</span><span {
        </span><span if</span>(<span $result</span> = <span $mysqli</span>->store_result()){<span //</span><span 如果还有还结果集,取当前的一个结果集返回</span>
            <span while</span>(<span $row</span> = <span $result</span>->fetch_row()){<span //</span><span 取一条记录</span>
                <span echo</span> <span $row</span>[0<span ];
            }
            </span><span $result</span>->free();<span //</span><span 当此结果集中无记录时,释放</span>
<span         }
        </span><span if</span>(<span $mysqli</span>->more_results()){<span //</span><span 如果还有一个结果集,则输出分隔线</span>
            <span echo</span> "<hr />"<span ;
        }
    }</span><span while</span>(<span $mysqli</span>-><span next_result());
} 


</span><span //</span><span 预处理</span>
<span $sql</span> = "SELECT `app`,`var` FROM `db_setting` WHERE `app`=? LIMIT 5"<span ;
</span><span $app</span> = 'article'<span ;
</span><span $stmt</span> = <span $mysqli</span>-><span stmt_init();
</span><span if</span>(<span $stmt</span>->prepare(<span $sql</span><span )) {
    </span><span $stmt</span>->bind_param("s", <span $app</span><span );
    </span><span $stmt</span>->execute();<span //</span><span 执行查询</span>
    
    <span $stmt</span>->bind_result(<span $field1</span>, <span $field2</span>); <span //</span><span 生成两个字段的结果集</span>
    <span while</span> (<span $stmt</span>-><span fetch()) {
        </span><span echo</span> '<br/>', <span $field1</span>,' => ', <span $field2</span><span ;
    }
    </span><span echo</span>'<hr/>'<span ;
    
    </span><span $app</span> = 'guestbook'<span ;
    </span><span $stmt</span>->execute();<span //</span><span 执行查询</span>
    <span $result</span> = <span $stmt</span>->get_result(); <span //</span><span 生成两个字段的结果集</span>
    <span while</span> (<span $row</span> = <span $result</span>-><span fetch_array(MYSQLI_NUM)){
        </span><span echo</span> <span $row</span>[0],' => ',<span $row</span>[1<span ];
    }
    
    </span><span $stmt</span>-><span close();
}
</span><span $mysqli</span>->close();
登录后复制

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440275.htmlTechArticle$host = 'localhost' = 'root' = '' = 'cmstop' = mysqli( , , , ) or ('无法连接到mysql:'. - = "SELECT CURRENT_USER(); SELECT * FROM `db_setting` LIMIT 5;" ( -multi_query( )){ ( =...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!