Jump to content

Search problem


Danthrax

Recommended Posts

:confused:

 

Hi everyone,

 

I was hoping someone would be able to help me out with this problem.

 

I keep on getting this error message whenever I enter a search term into my search bar...

 

"Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\Bazsite1\pages\Search.php on line 29

 

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Bazsite1\pages\Search.php on line 30

Search results

 

Sorry, your search for: " Black " returned zero results

 

Click here to try the search on google

couldn't execute query"

 

This is the form....

 

 

<form action="Search.php" method="POST">

    <input name="search" type="text" maxlength="50" id="search" />

    <input name="submit" type="submit" value="search" />

</form>

 

and

 

here is the PHP code....

 

<?php
$search = $_POST['search'];
$trimmed = stripslashes(trim($search)); 
$limit = 10;

if ($trimmed == "") 
{
echo"<p>Please enter a search... </p>";
exit;
}

if (!isset($trimmed)) 
	{
		echo "<p>We don't seem to have a search parameter! Please enter one!! </p>";
		exit;
	}
	//changed it from $search to $trimmed!




	$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE prodId"); //<-- have a feeling that $prods isn't right!

	$numresults = mysql_query($query);
	$numrows = mysql_num_rows($numresults);   


		if ($numrows == 0)

			{
				echo"<h4> Search results </h4>";
				echo"<p>Sorry, your search for: " ".$trimmed. " " returned zero results</p>"; 


			//google alternative

		echo "<p><a href=\"http://www.google.com/search?q=". $trimmed . "\" target=\"_blank\" title=\"Look up" . $trimmed . "on Google\">Click here</a> to try the search on google</p>";

			}

if (empty($s))
{
$s=0;	
}

$query .= "limit $s, $limit";
$result = mysql_query($query) or die ("couldn't execute query");

//display search 

echo "<p> You searched for: " ".$trimmed."a " </p>";

echo "results";
$count = 1 + $s;

while ($row = mysql_fetch_array($result))
{
	$title = $row["ProdId"]; 


echo "$count.)  $title"; 
$count++;
}

$currPage = (($s/$limit) + 1);

echo "<br />";


if ($s>=1)

{
$prevs = ($s - $limit);
print "  <a href = \"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10 </a>    ";
}

// calculate number of pages needing links
$pages = intval($numrows/$limit);

// calculate number of pages needing links
if($numrows%$limit)

{
$pages++;
}

if(!((($s+$limit)/$limit)==$pages) && $pages !=1)

{
	// not last page so give NEXT link
	$news = $s + $limit;
	echo "  <a href=\"$PHP_SELF?s=$new&q=$search\"> Next 10 >> </a>";

}

$a = $s + ($limit);

if ($a > $numrows) 
	{
		$a = $numrows; 
	}

	$b = $s + 1;

	echo "<p>Showing results $b to $a of $numrows</p>";

?>

 

and here's a copy of my products table from my database, I've taken out the useless stuff but it should just give you an idea of what fields are called what etc...

 

ProdId ProdName ProdDesc ProdImage price

1 Brown Bowl  

2 Kyushu Ceramic pot    

3 Clay lukens    

4 Horn acorn bowl    

5 Large Black brown ceramic bowl    

6 Painted turtle bowl    

7 Grey vase    

8 Black vase    

9 decorative ceramic vase    

10 Jasper Urn    

11 Lily Green, Brown and Blue vas    

12 Bysantine Turquoise Vase    

13 Japanese Cup    

14 Dragon Cup    

15 Painted Cup    

16 Ceramic cup and Saucer    

 

any help is greatly appreciated!!! :)

 

Thank you in advance and if you need anything to help solve this, I'll be more than happy to post it! :)

Link to comment
Share on other sites

You're calling mysql_query() twice.

 

Take out this line:

$numresults = mysql_query($query);

 

And change this:

$numrows = mysql_num_rows($numresults);

 

to:

$numrows = mysql_num_rows($query);

Link to comment
Share on other sites

You're already executing a mysql_query() call here:

<?php
$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE prodId"); //<-- have a feeling that $prods isn't right!
?>

so this line is getting the error:

<?php
$numresults = mysql_query($query);
?>

Change your code to:

<?php
$query = "SELECT * FROM products WHERE ProdName LIKE prodId";
$numresults = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>

 

Ken

Link to comment
Share on other sites

Thanx maq and kenrbnsn for your help!

 

It wouldn't excute the query, so I've changed it to...

 

<?php include("header.php"); ?>

<body id="body">
<div id="container1">

<?php
$search = $_POST['search'];
$trimmed = stripslashes(trim($search)); 
$limit = 10;

if ($trimmed == "") 
{
echo"<p>Please enter a search... </p>";
exit;
}

if (!isset($trimmed)) 
	{
		echo "<p>We don't seem to have a search parameter! Please enter one!! </p>";
		exit;
	}
	//changed it from $search to $trimmed!




	$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE $trimmed"); //<-- have a feeling that $prods isn't right!

	/*$numresults = mysql_query($query);*/
	$numrows = mysql_num_rows($query);  


		if ($numrows == 0)

			{
				echo"<h4> Search results </h4>";
				echo"<p>Sorry, your search for: " ".$trimmed. " " returned zero results</p>"; 


			//google alternative

		echo "<p><a href=\"http://www.google.com/search?q=". $trimmed . "\" target=\"_blank\" title=\"Look up" . $trimmed . "on Google\">Click here</a> to try the search on google</p>";

			}

if (empty($s))
{
$s=0;	
}

$query .= "limit $s, $limit";
$result = mysql_query($query) or die ("couldn't execute query");

//display search 

echo "<p> You searched for: " ".$trimmed."a " </p>";

echo "results";
$count = 1 + $s;

while ($row = mysql_fetch_array($result))
{
	$title = $row["ProdId"]; 


echo "$count.)  $title"; 
$count++;
}

$currPage = (($s/$limit) + 1);

echo "<br />";


if ($s>=1)

{
$prevs = ($s - $limit);
print "  <a href = \"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10 </a>    ";
}

// calculate number of pages needing links
$pages = intval($numrows/$limit);

// calculate number of pages needing links
if($numrows%$limit)

{
$pages++;
}

if(!((($s+$limit)/$limit)==$pages) && $pages !=1)

{
	// not last page so give NEXT link
	$news = $s + $limit;
	echo "  <a href=\"$PHP_SELF?s=$new&q=$search\"> Next 10 >> </a>";

}

$a = $s + ($limit);

if ($a > $numrows) 
	{
		$a = $numrows; 
	}

	$b = $s + 1;

	echo "<p>Showing results $b to $a of $numrows</p>";

?>
</div>
</body>

 

 

so basically, I've changed my query to this...

 

$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE $trimmed");

 

And so now it's outputting to the page...

"Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Bazsite1\pages\Search.php on line 30

Search results

 

Sorry, your search for: " Black " returned zero results

 

Click here to try the search on google

couldn't execute query

 

 

Link to comment
Share on other sites

I think your query is failing because you need single quotes around $trimmed in the LIKE clause.  I also added some error handling:

 

$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE '$trimmed'");
if($query)
{
  $numrows = mysql_num_rows($query);    
}
else
{
  echo "Error: " . mysql_error();
  die(); //or do whatever
}

Link to comment
Share on other sites

I really do appreciate all the help you're giving me Maq and Ken!

I've put that if...else statement into my code and it doesn't seem to be outputting any errors...

But, it's now outputting this to the page...

 

"Search results

 

Sorry, your search for: " Black " returned zero results

 

Click here to try the search on google

couldn't execute query"

 

Could it be that the variable $trimmed is the wrong variable to use? or could my query be completely wrong?

 

I'm still studying PHP so any help would be awesome...

 

thanx :)

Link to comment
Share on other sites

Sorry, your search for: " Black " returned zero results

 

Well that means that your search was valid but didn't yield any results. 

 

Could it be that the variable $trimmed is the wrong variable to use? or could my query be completely wrong?

 

That's really impossible for us to answer but, it does look like you would want to use '$search' instead.

Link to comment
Share on other sites

I thought that might be the case! I've tried $search before, but that was when I had it all wrong!

 

You guys really are awesome for helping me out with this and I don't expect you guys to do the impossible, I really appreciate the help you've given me so far!! it's a step in the right direction!

 

Thank you

 

:)

Link to comment
Share on other sites

No, I just get this now...

 

Problem with the query: Resource id #4

 

But that comes from this...

 

$numrows = mysql_num_rows($query) or die("Problem with the query: $query<br>" . mysql_error()); 

 

and I don't understand what that means, I've put it through Google and alot people have similar problems with that, but it still doesn't make sense!

 

I don't know why it's doing this.... I've put in $search and that doesn't work either! hmmm.... :)

 

 

Link to comment
Share on other sites

Hi Maq,

 

Yeah sure, is this what you're after?

 

:)

 

<?php include("header.php"); ?>

<body id="body">
<div id="container1">

<?php
$search = $_POST['search'];
$trimmed = stripslashes(trim($search)); 
$limit = 10;

if ($trimmed == "") 
{
echo"<p>Please enter a search... </p>";
exit;
}

if (!isset($trimmed)) 
	{
		echo "<p>We don't seem to have a search parameter! Please enter one!! </p>";
		exit;
	}
	//changed it from $search to $trimmed!




	$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE '$search'"); 

if($query)
{
  $numrows = mysql_num_rows($query);   
}
else
{
  echo "Error: " . mysql_error();
  die(); 
}



	/*$numresults = mysql_query($query);*/
	$numrows = mysql_num_rows($query) or die("Problem with the query: $query<br>" . mysql_error());  


		if ($numrows == 0)

			{
				echo"<h4> Search results </h4>";
				echo"<p>Sorry, your search for: " ".$trimmed. " " returned zero results</p>"; 


			//google alternative

		echo "<p><a href=\"http://www.google.com/search?q=". $trimmed . "\" target=\"_blank\" title=\"Look up" . $trimmed . "on Google\">Click here</a> to try the search on google</p>";

			}

if (empty($s))
{
$s=0;	
}

$query .= "limit $s, $limit";
$result = mysql_query($query) or die ("couldn't execute query");

//display search 

echo "<p> You searched for: " ".$trimmed."a " </p>";

echo "results";
$count = 1 + $s;

while ($row = mysql_fetch_array($result))
{
	$title = $row["ProdId"]; 


echo "$count.)  $title"; 
$count++;
}

$currPage = (($s/$limit) + 1);

echo "<br />";


if ($s>=1)

{
$prevs = ($s - $limit);
print "  <a href = \"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10 </a>    ";
}

// calculate number of pages needing links
$pages = intval($numrows/$limit);

// calculate number of pages needing links
if($numrows%$limit)

{
$pages++;
}

if(!((($s+$limit)/$limit)==$pages) && $pages !=1)

{
	// not last page so give NEXT link
	$news = $s + $limit;
	echo "  <a href=\"$PHP_SELF?s=$new&q=$search\"> Next 10 >> </a>";

}

$a = $s + ($limit);

if ($a > $numrows) 
	{
		$a = $numrows; 
	}

	$b = $s + 1;

	echo "<p>Showing results $b to $a of $numrows</p>";

?>
</div>
</body>

Link to comment
Share on other sites

You put the or die on the wrong line.

 

Change this line to:

$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE '$search'"); 

 

to:

$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE '$search'") or die("Error: " . mysql_error()); 

 

 

and this line:

$numrows = mysql_num_rows($query) or die("Problem with the query: $query
" . mysql_error());  

 

to:

$numrows = mysql_num_rows($query);  

Link to comment
Share on other sites

See if this fully commented script is what you are trying to do.

 

I tried to comment it up, so you can see what it is doing.  Compare it to your current script, to see what you can change.

UN-TESTED.

<?php include("header.php"); ?>
<body id="body">
<div id="container1">
<?php
$search = $_POST['search'];
$search = (isset($_GET['q'])) ? $_GET['q'] : $search;
$trimmed = stripslashes(trim($search));

$limit = 10;
if ($trimmed == "") 
{

echo"<p>Please enter a search... </p>";

exit;
}

if (!isset($trimmed)) 


{



echo "<p>We don't seem to have a search parameter! Please enter one!! </p>";



exit;


}

$safe_search = mysql_real_escape_string($trimmed);
//changed it from $search to $trimmed!
echo"<h4> Search results </h4>";
//-->Paganate info <--
$numberPerpage = 10; //Number you want to show per page.
$sql = "SELECT COUNT(*) FROM products WHERE ProdName LIKE '$safe_search'"; //tell Mysql what you want counted.
$result = mysql_query($sql); //execute query.
$row = mysql_fetch_row($result); //get the result, only 1 row, no need for a loop.
$numberOfrows = $row[0]; //set result to a variable.

$pages = ceil($numberOfrows / $numberPerpage); //calculate number of pages.
$pageNo = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; //get the current page number, if it isn't set, set it to 1;
$limit = 'LIMIT ' . ($pageNo - 1) * $numberPerpage . ',' . $numberPerpage; //build our limit.
//-->End Paganate info <--

//Pulling info we need, from database, including limit's.
if($numberOfrows > 0) { //if we have a row count.
$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE '$safe_search' $limit") or die('Error: ' . mysql_error()); //perform query, or die trying.
echo "<p> You searched for: " ".$trimmed."a " </p>";
echo 'Showing ' . (($numberPerpage * $pageNo) - $numberPerpage) . ' - ' . ($numberPerPage * $pageNo) . ' of ' . $numberOfrows . ' results';
$count = 1 + $s;
while ($row = mysql_fetch_array($result)) { //run through a loop to make sure we got all of them.
	$title = $row["ProdId"]; 
	echo "$count.)  $title"; 
	$count++;
}
}
else { // Send back 0 results, and give a google link.


echo"<p>Sorry, your search for: " ".$trimmed. " " returned zero results</p>"; 

//google alternative

echo "<p><a href=\"http://www.google.com/search?q=". $trimmed . "\" target=\"_blank\" title=\"Look up" . $trimmed . "on Google\">Click here</a> to try the search on google</p>";

}

echo "<br />";

if($numberOfrows > 0) { //print links only if there is a row count.
//Set pages.
if($pageNo <= 1) { //If there are no pages to go back to: print just the words.
echo "  << Prev 10    ";
}
else { //If there is a link back to the previous, show a link.
echo "  <a href = \"?page=". ($pageNo - 1) . "&q=$search\"><< Prev 10 </a>    ";
}
if($pageNo < $pages) { //If there are more pages to look at, show a link to the next one.
echo "  <a href=\"?page=" . ($pageNo + 1) . "&q=$search\"> Next 10 >> </a>";
}
else { //If there is no more pages, just print the words.
echo "   Next 10 >>";
}
}
?>
</div>
</body>

Link to comment
Share on other sites

Oh jeez, you're right! :D

 

I was doing all this at around 1 am last night, so I was half asleep! LOL!! :)

 

Thanks, Now I've realised that I've made a mistake with the If statement on line 45!

 

Parse error: syntax error, unexpected T_IF in C:\xampp\htdocs\Bazsite1\pages\Search.php  on line 45

Link to comment
Share on other sites

Hi Jcbones, Maq and Ken,

 

Thank you jcbones, I've got alot out of that script. That's really cool and it actually kinda works!

 

If you don't mind me asking for a little more help as I understand alot of what's happening with that script, but I'm still a learner and to me it looks like it should work perfectly!

 

maybe there's just some variables I need to change or something, but I'm having some trouble finding the problem....

 

Anyway, here's what's outputting to the screen...

 

Search results

 

Sorry, your search for: " Black vase " returned zero results

 

Click here to try the search on google

 

Which works great, except that I've actually do have something called "Black vase" in my products table, So, it's passing the variable through, but I don't think it's actually searching the database, So I presumed it must be something to do with the Sql Queries. But I've checked them and they look like they should work, but it doesn't! ...so I'm at a loss! :D

 

I can't believe how helpful you've all been in helping me with this! I really appreciate everyones help. And I hope I'm not asking for too much!

 

:)

 

 

Link to comment
Share on other sites

If you want to match any string with that value in it you have to use:

 

LIKE '%{$safe_search}%'";

 

If $safe_search is 'black' then this will match black along with any amount of characters before and after.

Link to comment
Share on other sites

Hi Guys,

 

Maq: Oh yeah, thanx!! :)

 

jcbones: I've echoed out $Sql (the easiest way to explain will be just to show you what I've done)....

 

So, I've echoed $sql,

 

$sql = "SELECT COUNT(*) FROM products WHERE ProdName LIKE '%{$safe_search}%'";

 

echo "$sql";

 

output: SELECT COUNT(*) FROM products WHERE ProdName LIKE '%Black%'

 

Sorry, your search for: " Black " returned zero results

 

Click here to try the search on google

 

So, that seems to work, so I've tried to echo out the next query, which was $query....

 

$query = mysql_query("SELECT * FROM products WHERE ProdName LIKE '$safe_search' $limit") or die('Error: ' . mysql_error()); //perform query, or die trying.

 

echo $query;

 

output:  Search results

 

Sorry, your search for: " Black " returned zero results

 

Click here to try the search on google

 

and as you can see nothing came out, So I think it might have something to do with $query not accessing the database properly. But I don't understand, that query seems right!?!?!?

 

What do you guys think??? :)

Link to comment
Share on other sites

If you want to print out the query, you must assign a variable to it first:

 

$query = "SELECT * FROM products WHERE ProdName LIKE '$safe_search' $limit";
$result = mysql_query($query) or die('Error: ' . mysql_error()); //perform query, or die trying.
echo "Query: " . $query;

 

(Now, you must use $result to access the result set)

 

And again, if you want to match any string that contains $safe_search you must use the '%'s.

Link to comment
Share on other sites

Hi Maq,

 

Ok, this is what I've done:

 

$query = "SELECT * FROM products WHERE ProdName LIKE '$safe_search', '$limit'";

$result = mysql_query($query) or die('Error: ' . mysql_error()); //perform query, or die trying.

echo "Query: " . $query;

 

I've put single quotes around $limit for consistancy, But it still doesn't output anything!

Link to comment
Share on other sites

:confused:

 

Ok.... thanx for you help, I really appreciate all the help I got from you guys!

I never did work it out, in fact the code has now become even more confusing than ever!! It should work, but it doesn't work... I don't understand!

 

I can't keep asking you guys for help, I've probably asked for too much anyway or you can't help me.... :shrug:

 

so, thank you kindly!! 

 

:shy:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.