Num of rows php!

Num of rows php!
0

Hello campers! <3
so i am new to coding, i wrote this code for making a search field and i keep having this problem can anyone help me

my code :

<?php include 'header.php'; ?>
<h1>Search Page</h1>
<div class="article-container">

<?php 

	if ($_POST['submit-search']){
		$Search = mysqli_real_escape_string($conn,$_POST['search']);
		$sql = "SELECT * articals WHERE a_title LIKE '%$search%' OR a_artical LIKE '%$search%'OR a_author LIKE '%$search%' a_date LIKE '%$search%' ";
		$result = mysqli_query($conn,$sql);
		$queryReslut = mysqli_num_rows($result);

		if ($queryReslut > 0 ) {
			while ($row = mysqli_fetch_assoc($reslut)) {
				echo "<div> 
								<h3>".$row['a_title']."</h3>
								<p>". $row['a_text']."</p>
								<p>". $row['a_date']."</p>
								<p>". $row['a_author']. "</p>

							</div>";
			}
		} else if ($queryReslut = 0) {
			echo "There are no results matching your search!";
		}
	}

		?>

The error i get :
Warning : mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\Program Files\Ampps\www\test\searchfield\index.php on line 15

Thank you very much sir for your really speed replay but i did not really get it , can you explain please?

Take a look at the Return Values section of this page, on php’s documentation: https://www.php.net/manual/en/mysqli.query.php

Here’s the thing: there’s an error in your SQL statement, which is causing mysql_query() to fail. When it fails, it returns a Boolean false value, rather than the mysqli_result return value that mysql_num_rows() is looking for.

Either fix the SQL, or handle the error coming from your mysql_query() call.

3 Likes

Thank you very much sir!
i will try to figure it out

Not tested, but it should help.

<?php 

if ($_POST['submit-search']) {
	$conn = new mysqli("localhost", "my_user", "my_password", "my_db");

	// Check connection
	if ($conn->connect_error) {
	  echo "Failed to connect to MySQL: " . $conn->connect_error;
	  exit();
	}

	$search = mysqli_real_escape_string($conn,$_POST['search']);
	$sql = "SELECT * FROM articals WHERE a_title LIKE '%$search%' OR a_artical LIKE '%$search%' OR a_author LIKE '%$search%' OR a_date LIKE '%$search%' ";
	$result = mysqli_query($conn, $sql);
	$resultNrows = mysqli_num_rows($result);

	if ($resultNrows > 0 ) {
		while ($row = mysqli_fetch_assoc($result)) {
			echo "<div> 
					<h3>".$row['a_title']."</h3>
					<p>". $row['a_text']."</p>
					<p>". $row['a_date']."</p>
					<p>". $row['a_author']. "</p>
				</div>";
		}
	} else if ($resultNrows = 0) {
		echo "There are no results matching your search!";
	}
	
	mysqli_free_result($result);
	mysqli_free_result($resultNrows);
}

?>

2 Likes

it worked!
but i did not get the articals not in the index page or in the artical page where it shows after searching
i am new to all of this, it is a little bit hard but i am getting throw
Thank you so much!

The error in the SQL was just at the end, the last option wasn’t led by an OR. Once the SQL was fixed, it should work fine. Wouldn’t be a bad idea to error-check that though:

$result = mysqli_query($conn, $sql);
if ($result == false){
  // Here, you have an error, handle it!
} else {
  // Here, we KNOW that result isn't false, so it's safe!
  $resultNRows = mysqlI_num_rows($result);
  // ... and on and on.
1 Like

Thank you for your advice sir, i will try my best and i forgot to check for errors first then go on with coding , thank you very much!

I think you forgot “OR” or maybe and “AND” inside your query right before the a_date ?

1 Like

Great catch, @romi_devx - that’s exactly the error I was hinting at with “there’s an error in your SQL”. Precisely why the query was failing.

That said, it’s a great example of why we need error checking within our code… :wink: