1mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
2$link = mysqli_connect("host", "username", "password","db_name");
3mysqli_set_charset($link, "utf8mb4");
4
5$result = mysqli_query($link, "SELECT count(*) FROM blackandwhite");
6$num_rows = mysqli_fetch_row($result)[0];
7
8echo "$num_rows Rows\n";
9
1<?php
2/*
3Explination
4The mysqli_num_rows() function is an inbuilt function in PHP
5which is used to return the number of rows present in the result set.
6It is generally used to check if data is present in the database or not.
7To use this function, it is mandatory to first set up the connection with the MySQL database.
8*/
9 // Setting up connection with database Geeks
10 $con = mysqli_connect("localhost", "root", "", "testing");
11
12 // Check connection
13 if (mysqli_connect_errno()) {
14 echo "Database connection failed.";
15 }
16 // Fetch Query
17 $query = "SELECT Username, Password FROM users";
18
19 // Execute the query and store the result set
20 $result = mysqli_query($con, $query);
21
22 if ($result) {
23 // it return number of rows in the table.
24 $row = mysqli_num_rows($result);
25 if ($row) {
26 printf("Number of row in the table : " . $row);
27 }
28 // close the result.
29 mysqli_free_result($result);
30 }
31
32// Output : Number of row in the table : 5
33?>
34
1// Fetch Query
2$query = "SELECT user_name from registered_users where user_name like '%ank%'";
3
4// Execute the query and store the result set
5$result = mysqli_query($con, $query);
6
7if ($result) {
8 // it return number of rows in the table.
9 $row = mysqli_num_rows($result);
10}