一
首先需定義一個資料庫介面函數,可單獨存為一個php頁
<code><span><span><?php</span><span><span>function</span><span>db_connect</span><span>()</span> {</span><span>$result</span> = <span>new</span> mysqli(<span>'数据库地址'</span>, <span>'账号'</span>, <span>'密码'</span>, <span>'库名'</span>);<span>//连接数据库</span><span>if</span> (!<span>$result</span>) { <span>return</span><span>false</span>;<span>//连接失败</span> } <span>return</span><span>$result</span>;<span>//返回数据库对象</span> } <span>?></span></span></code>
二
在另一php頁面中,首先需使用include函數
介面函數能被正確地呼叫
<code><span><span><?php</span><span><span>function</span><span>output_rows</span><span>(<span>$seller_id</span>)</span>{</span><span>$conn</span> = db_connect();<span>//连接数据库</span><span>if</span> (!<span>$conn</span>) {<span>//无法连接数据库</span><span>echo</span><span>"can not connect mysql"</span>; <span>return</span><span>'fail_mysql'</span>; } <span>$result</span> = <span>$conn</span>->query(<span>"SELECT * FROM t_order WHERE seller_id= '"</span>. <span>$seller_id</span>.<span>"' "</span>);<span>//查询数据</span><span>echo</span><span>$result</span>->num_rows; <span>//输出行数</span> } <span>?></span></span></span></code>
透過呼叫上述定義的函數即可顯示sql查詢結果的行數,上述函數透過$result = $conn->query("SELECT * FROM t_order WHERE seller_id= '". $ seller_id."' ");
進行資料庫的查詢,查詢t_seller
表中seller_id
等於$seller_id
的所有行,並將結果集賦給$res$result,再透過使用$res num_rows
可取得查詢結果的行數,以echo
進行輸出顯示').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i