Jump to content

[SOLVED] mysql_num_rows problem


Rens

Recommended Posts

Hi guys,

 

I'm kinda new to PHP and new to this forum as well.

For a school project i need to make a mysql database with pictures in it.

those pictures should be findable on some words, if the user fills in "red" for example, then the database has to return all pictures that has the word red with it.

 

Now this i managed to do and it works. However, if there are no words found, the database will return nothing obviously. I then want to echo something like "sorry, nothing found, try again".

 

I figured i can do that with mysql_num_rows, but after 3 hours of work, i can't get it to work.

 

Here is my code:

 

<div align="center">
<h1>Welcome on my database</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Type in your entry: <br>
<input type="text" name="trefwoord"<br><br>
<input type="submit" value="Search!">
</form>

<?php
$host = 'localhost';
$gebruiker = 'root';
$pass = '';
$databasename = 'database1';
$query = "SELECT `urlplaatje` FROM `plaatjes` WHERE `trefwoord` LIKE '%".$_POST['trefwoord']."%'";


	// stap 1) make a connection
	if ($db = mysqli_connect($host, $gebruiker, $pass)){
		echo "Stap 1) the connection with MySQL - $host is succeeded<br />";
		echo "logged in as: $gebruiker<br />";
	}
	else{
		echo "The connection with MySQL cannot be opened";
	}

	// stap 2) check if the database can be opened
	if (mysqli_select_db($db, $databasename)){
		echo "Stap 2) Database selected: $databasename<br />";
	}
	else{
		echo "ERROR: the database $databasename could not be opened";
		exit;
	}

	// stap 3) execute the query
	if ($result = mysqli_query($db, $query)){
		echo "Stap 3) the query \"$query\" has been completed<br />";
	}
	else{
		echo "ERROR: there has been an error with query: $query";
		exit;
	}

	// stap 4) print the results to the screen
	echo "<h2>Stap 4) inhoud:</h2>";
	if ($_POST["trefwoord"] <> "") {
	while ($rij = mysqli_fetch_array($result)){
		echo "<img src=\"{$rij['urlplaatje']}\" <br />";
		if (mysql_num_rows($result) == 0) //this is line 66
		{
			echo "No comparison found , try to find something else!";
		}
	}
}								
	else 
	{
		echo "Please fill in a word you want to search on";
	}
?>

</div>

 

I translated some stuff into english, so it would be easier for you guys to understand, variables are still dutch though.

 

now my error will be:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\wamp\www\index.php on line 66

No comparison found , try to find something else!

 

Is there something wrong with my code?

 

PS: I am using wampserver with mysql version 5.0.51b

 

I hope you guys could help me out, thanks in advance!!  ;D  :D

 

Rens

 

 

Link to comment
https://forums.phpfreaks.com/topic/111864-solved-mysql_num_rows-problem/
Share on other sites

put this below your query

$numrows = mysql_num_rows($query;

 

Then, replace

if (mysql_num_rows($result) == 0) //this is line 66
		{
			echo "No comparison found , try to find something else!";
		}

with this

if($numrows == 0)
{
echo "No comparison found , try to find something else!";
}

 

aaah how stupid can i be haha

Thanks guys!

 

i changed the code to:

 

// stap 4) print the results to the screen
	if ($_POST["trefwoord"] <> "") {
	while ($rij = mysqli_fetch_array($result)){
		echo "<img src=\"{$rij['urlplaatje']}\" <br />";
		}


		if (mysqli_num_rows($result) == 0)
		{
			echo "Sorry, " . $_POST['trefwoord'] . " has not been found in the database";
		}
	}

	else 
	{
		echo "Please fill in something to search for!";
	}

 

Now it works just smoothly! thanks guys :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.