Home >Backend Development >PHP Tutorial >1. PHP - Get the number of rows in MySQL database query results. MySQL database download 64-bit. SQL database instance download. SQL database patch download.
一
First you need to define a database interface function, which can be saved as a separate php page
<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>
二
In another php page, you first need to use the include function
to include the above php page, that is, you need to ensure the above The interface function can be called correctly
<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>
The number of rows in the SQL query result can be displayed by calling the function defined above. The above function is passed$result = $conn->query("SELECT * FROM t_order WHERE seller_id= '". $seller_id."' ");
Perform a database query, query all rows in the t_seller
table where seller_id
is equal to $seller_id
, and assign the result set to $result
, and then use $result- >num_rows
can get the number of rows in the query result, and use echo
to output and display
The above introduces 1. PHP-getting the number of rows of MySQL database query results, including the content of Mysql database. I hope it will be helpful to friends who are interested in PHP tutorials.