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.

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.

WBOY
WBOYOriginal
2016-07-29 08:53:571207browse

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 functionto 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_rowscan get the number of rows in the query result, and use echo to output and display

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn