Hướng dẫn mysqli_num_rows là gì

Hàm mysqli_num_rows[] sẽ trả về số hàng trong tập hợp kết quả truyền vào.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: mysqli_num_rows[ $result];

Trong đó:

  • $result là tập hợp kết quả trả về từ các hàm mysqli_query[], mysqli_store_result[] hoặc mysqli_use_result[].

Kết quả trả về

Hàm sẽ trả về số nguyên đại diện cho số hàng có trong kết quả.

Bài viết này được đăng tại [free tuts .net]

Ví dụ

Cách sử dụng hàm mysqli_num_rows[]:

$con=mysqli_connect["localhost","my_user","my_password","my_db"];
// Check connection
if [mysqli_connect_errno[]]{
	echo "Failed to connect to MySQL: " . mysqli_connect_error[];
}

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if [$result=mysqli_query[$con,$sql]]{
  // Return the number of rows in result set
	$rowcount=mysqli_num_rows[$result];
	printf["Result set has %d rows.\n",$rowcount];
  // Free result set
	mysqli_free_result[$result];
}

mysqli_close[$con];

Tham khảo: w3schools.com

my_personal_contacts

  • Kết nối với MySQL bằng phpMyAdmin, v.v ... 
  • Tạo cơ sở dữ liệu có tên my_person_contacts
  • Thực thi tập lệnh hiển thị bên dưới để tạo bảng và chèn một số dữ liệu demo
  • CREATE TABLE IF NOT EXISTS `my_contacts` [

      `id` int[11] NOT NULL AUTO_INCREMENT,

      `full_names` varchar[255] NOT NULL,

      `gender` varchar[6] NOT NULL,

      `contact_no` varchar[75] NOT NULL,

      `email` varchar[255] NOT NULL,

      `city` varchar[255] NOT NULL,

      `country` varchar[255] NOT NULL,

    ] ENGINE = InnoDB  DEFAULT CHARSET = latin1 AUTO_INCREMENT = 5 ;

    INSERT INTO `my_contacts` [

    [1, 'Zeus', 'Male', '111', 'zeus@olympus.mt.co', 'Agos', 'Greece'],

    [2, 'Anthena', 'Female', '123', 'anthena@olympus.mt.co', 'Athens', 'Greece'],

    [3, 'Jupiter', 'Male', '783', 'jupiter@planet.pt.co', 'Rome', 'Italy'],

    [4, 'Venus', 'Female', '987', 'venu @planet.pt.co', 'Mars', 'Italy'];


    Chủ Đề