Maison > Article > développement back-end > php 操作 mysql 预处理(未完待续)_PHP教程
<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();