首頁  >  文章  >  後端開發  >  php 操作 mysql 预处理(未完待续)_PHP教程

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

WBOY
WBOY原創
2016-07-20 11:14:31954瀏覽

<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( )){ ( =...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn