一
首先需定义一个数据库接口函数,可单独存为一个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></span></code>
二
在另一php页面中,首先需使用include函数
包含上述php页面,也即需保证上述接口函数能被正确调用
<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
的所有行,并将结果集赋给$result
,再通过使用$result->num_rows
可取得查询结果的行数,用echo
进行输出显示
以上就介绍了1、PHP-获取MySQL数据库查询结果行数,包括了Mysql数据库方面的内容,希望对PHP教程有兴趣的朋友有所帮助。