Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
mysqli_num_rows() expects parameter 1 to be in mysqli_result,bool given in

PHP
$search <hr size="1"><br>";
$con=mysqli_connect("localhost","root","","search");
mysqli_select_db($con,"search");
//if($con)
	//echo"connection success";
//else
	//if(!$con)
		//echo"connection failed";
 
$search_exploded = explode (" ", $search);

 foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
 
}
 
$construct ="SELECT * FROM searchengine WHERE $construct";
$run= mysqli_query($con,$construct);

$foundnum= mysqli_num_rows($run);
 if ($foundnum==0)
echo "Sorry, there are no matching result for $search.<br><br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'<br>2. Try different words with similar
 meaning<br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";
 
while($runrows = mysqli_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
 
echo "
<a href="$url">$title</a><br>
$desc<br>
<a href="$url">$url</a></p><p>
";
 
}
}
 
}
}
 
?></p>


What I have tried:

<?php
	define("SITE_ADDR", "http://localhost/tutorials/search_engine");
	include("./include.php");
	$site_title = 'Simple Search Engine | HeyTuts.com tutorials';
?>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width">
		
		<title><?php echo $site_title; ?></title>
		
		<!-- link to the stylesheets -->
		<link rel="stylesheet" type="text/css" href="./main.css"></link>
	</head>
	
	<body>
		
		<div id="wrapper">
			
			<div id="top_header">
				<div id="nav">
					<a href="<?php echo SITE_ADDR;?>/new_entry.php">New Entry</a>
				</div>

				<div id="logo">
					<h1><a href="<?php echo SITE_ADDR;?>">simple search engine</a></h1>
				</div>
			</div>

			<div id="main" class="shadow-box"><div id="content">
				
				<center>
				<form action="" method="GET" name="">
					<table>
						<tr>
							<td><input type="text" name="k" placeholder="Search for something" autocomplete="off"></td>
							<td><input type="submit" name="" value="Search" ></td>
						</tr>
					</table>
				</form>
				</center>

				<?php

					// CHECK TO SEE IF THE KEYWORDS WERE PROVIDED
					if (isset($_GET['k']) && $_GET['k'] != ''){
						
						// save the keywords from the url
						$k = trim($_GET['k']);

						// create a base query and words string
						$query_string = "SELECT * FROM search_engine WHERE ";
						$display_words = "";

						// seperate each of the keywords
						$keywords = explode(' ', $k); 
						foreach($keywords as $word){
							$query_string .= " keywords LIKE '%".$word."%' OR ";
							$display_words .= $word." ";
						}
						$query_string = substr($query_string, 0, strlen($query_string) - 3);

						// connect to the database
						$conn = mysqli_connect('localhost','root',"",'tutorials');

						$query = mysqli_query($conn, $query_string);
						$result_count = mysqli_num_rows($query);

						// check to see if any results were returned
						if ($result_count > 0){
							
							// display search result count to user
							echo '<br /><div class="right">'.$result_count.' results found</div>';
							echo 'Your search for '.$display_words.' <hr /><br />';

							echo '<table class="search">';

							// display all the search results to the user
							while ($row = mysqli_fetch_assoc($query)){
								
								echo '<tr>
									<td><h3><a href="'.$row['url'].'">'.$row['title'].'</a></h3></td>
								</tr>
								<tr>
									<td>'.$row['blurb'].'</td>
								</tr>
								<tr>
									<td>'.$row['url'].'</td>
								</tr>';
							}

							echo '</table>';
						}
						else
							echo 'No results found. Please search something else.';
					}
					else
						echo '';
				?>

			</div></div>

			<div id="footer">
				
				
				<div class="clear"></div>
			</div>

		</div>

	</body>
</html>
Posted
Updated 27-May-22 4:42am
v2

Quote:
mysqli_num_rows() expects parameter 1 to be in mysqli_result,bool given in

As per documentation of PHP: mysql_query - Manual[^] - when the query statement fails the call returns the boolean value FALSE.

Not able to locate the call in your bunch of code provided. But, it means, your uery is returning FALSE and you expect a different type and have code based on it.

Handle the condition based on returned data appropriately.
 
Share this answer
 
The response you received from your query is that the query failed.. That means you need to check the TSQL you are generating for errors.

Do this by printing it to the screen (instead of running the query). Inspect each for errors and if you don't see any, try to run them in the MySQL environment where you'll be able to see the results directly. Use that to fix your query and then use that to fix your code.


 
Share this answer
 
$delete=mysqli_query($conn,"DELETE FROM studentinfo WHERE id='$id'");
            if(mysqli_num_rows($delete)>0)
            {
                echo "not don";
                header('location:student.php');
            }
            else{
                echo "done delete.";
                header('location:student.php');
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900