Jump to content

Search engine code


Johnnyboy123

Recommended Posts

Can someone help me with my code. Used a tutorial as aid but still struggling. Basically I have a table called student. I created a search engine so that if a user searches for either the sname,fname or sno of a student then that student would appear. Apart from some notices I got it working and displaying students when searching for only their sname. Now, I want to be able to make it possible to find a student by searching either sname,fname or sno and I'm getting errors. Can anyone have a look at my code:

 

<?php

//get data
$button = $_GET ['submit'];
$search = $_GET ['search'];

if (!$button)
echo "You didn't submit a term.";
else
{
if (strlen($search)<=0)
	echo "search term too short.";
	else
	{
		echo "You searched for <b>$search</b><hr size='1'>";
		include 'includes/config.php';
		include 'includes/functions.php';

		connect();




			//explode search term
			$search_exploded = explode(" ",$search);

			foreach($search_exploded as $search_each)
			{
			//construct query
			$x++;                                                                                         //Line 30
			if ($x==1)
			$construct .= "sname,fname,sno LIKE '%$search_each%'";      //Line 32
			else
			$construct .= "OR sname,fname,sno LIKE '%$search_each%'";



			}



	$construct = "SELECT * FROM student WHERE $construct";
	$run = mysql_query($construct);
	$foundnum = mysql_num_rows($run);                                      //Line 44

	if ($foundnum==0)
		echo "No results found.";
		else
		{
		echo"$foundnum results found!<p>";

		while ($runrows = mysql_fetch_assoc($run))
		{
		//get data
		$sname = $runrows['sname'];
		$fname = $runrows['title'];
		$sno = $runrows['sno'];

		echo "
		<b>$fname</b>
		$sname
		$sno";

		}
	}
	}
}
?>

 

Errors:Notice: Undefined variable: x in C:\Program Files\EasyPHP-5.3.3\www\Project\search.php on line 30

 

Notice: Undefined variable: construct in C:\Program Files\EasyPHP-5.3.3\www\Project\search.php on line 32

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\search.php on line 44

No results found.

 

First time attempting this, so any tips on what I did wrong would be great. Thank you in advance.

Link to comment
Share on other sites

Try the following code...

<?php

//get data
$button = $_GET ['submit'];
$search = $_GET ['search'];

if (!$button)
echo "You didn't submit a term.";
else
{
if (strlen($search)<=0)
	echo "search term too short.";
	else
	{
		echo "You searched for <b>$search</b><hr size='1'>";
		include 'includes/config.php';
		include 'includes/functions.php';

		connect();




			//explode search term
			$search_exploded = explode(" ",$search);
			$construct = '';
			$x = 0;

			foreach($search_exploded as $search_each)
			{
			//construct query
			$x++;
			if ($x==1)
			$construct .= "sname,fname,sno LIKE '%{$search_each}%'";
			else
			$construct .= " OR sname,fname,sno LIKE '%{$search_each}%'";

			}



	$construct = "SELECT * FROM student WHERE $construct";
	$run = mysql_query($construct);
	$foundnum = mysql_num_rows($run);

	if ($foundnum==0)
		echo "No results found.";
		else
		{
		echo"$foundnum results found!<p>";

		while ($runrows = mysql_fetch_assoc($run))
		{
		//get data
		$sname = $runrows['sname'];
		$fname = $runrows['title'];
		$sno = $runrows['sno'];

		echo "
		<b>$fname</b>
		$sname
		$sno";

		}
	}
	}
}
?>

 

Tell me how it goes buddy :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

Before your foreach on line 27, add a $x = 0; $construct = ''; That will define the variable before your script attempts to increment, check or add to it.

 

Echo out $construct before you execute the query. Also, try this out

 

$run = mysql_query($construct) or die(mysql_error());

 

The error will tell you where your query is going wrong.

Link to comment
Share on other sites

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\search.php on line 44

No results found.

 

that means it is not connecting to the database propibly and like xyph said to use the error thing to check.

 

try this

//explode search term
			$search_exploded = explode(" ",$search);
			$x = 0;
			foreach($search_exploded as $search_each)
			{
			//construct query
			$x++;
			if ($x==1)
			$construct = "sname,fname,sno LIKE '%{$search_each}%'";
			else
			$construct .= " OR sname,fname,sno LIKE '%{$search_each}%'";

			}

 

Link to comment
Share on other sites

Thanks guys, I adjusted it with the code that matthew provided, which cleared up all the notices. I used die(mysql_error()); to find the error. Seems the error is:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fname,sno LIKE '%sdfgf%'' at line 1

 

which refers to:

<?php
if ($x==1)
$construct = "sname,fname,sno LIKE '%{$search_each}%'";
else
$construct .= " OR sname,fname,sno LIKE '%{$search_each}%'";
}
?>

 

When I only use 1 field however e.g $construct = "sname LIKE '%{$search_each}%'";

It seems to work fine. The original tutorial from where I worked off also only used 1 field. I believe the tut was based on using entered info from only 1 field to find a student. However, I want to be able to use either any 1 of 3 fields: either sname,fname or sno. Any ideas?

 

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.