Php Search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Php Search

Post by ampersand »

I'm a newbie trying to make a search engine, but after I submit the form it says that there is something wrong with line 47, could anyone help me with this please :)


This is line 47: while ($row = mysql_fetch_object ($query_result))

Code: Select all

<?php require_once('../Connections/cds.php'); ?>

<?php
$db_connection = mysql_connect($hostname_cds , $username_cds , $password_cds) or die ("Could not connect to database");
mysql_select_db ($database_cds , $db_connection) or die ("Could not find database")
?>
<?php
/* HEADER STUFF*/
print "<title>Search Results</title>";
print "<link href="../style.css" rel="stylesheet" type="text/css">";


/* TABLE #1 */
	print  "<table width="100%" align="center" cellpadding="5" cellspacing="0">";
	print  "<tr>";
	print  "<td width="50%" class="small">Search Results for $search </td>";
	print  "<td width="50%" align="right"><a href="javascript:history.back()" onclick="history.back();return false">&#1111;Back]</a></td>";
  	print  "</tr>";
  	print  "<tr>";
  	print  "<td height="25" colspan="2" class="small"></td>";
  	print  "</tr>";
  	print  "<tr align="center">";
  	print  "<td colspan="2" class="small">";
/* TABLE #2 */
	print "<table width="100%" border="0" cellspacing="0" cellpadding="0">";
	print "<tr bgcolor="#FF0000">";
	print "<td width="60%">Title</td>";
	print "<td width="20%">CD</td>";
	print "<td width="20%">Category</td>";
	print "</tr>";
		
if (($search == "") || ($search == " ")) 
	
	&#123;
	print ("Don't you try be a smart ass again, ok?");
	exit;	
	&#125;

if (($search <> "") || ($search <> " "))

		$category_query = "SELECT * FROM cd, sw, cat WHERE (sw.sw_title like "$search%") 
							AND (sw.cat_id = "$cat" ) 
							AND (sw.cd_id = cd.cd_id) ORDER BY (sw.sw_title "$radDirection")";
							
		$query_result = mysql_query ($category_query, $db_connection) or die("There's some spooky monkey business going on..");

		while ($row = mysql_fetch_object ($query_result))
		&#123;
			print	"<tr>";
			print	"<td>" . $row->sw_title . "</td>";
			print	"<td>" . $row->cd_title . "</td>";
			print	"<td>" . $row->cat_title . "</td>";
			print	"</tr>";
		&#125; 

/* TABLE #1 */
		print "<tr align="center">";
		print "<td colspan="2"></td>";
		print "</tr>";
		print "</table>";

?>

<?php
mysql_close($db_connection);
?>
There might be more errors here :) :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It does help if you tell us what the error message says.

Mac
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

it says nothing, but it dos not find any records either, even I know they are there.
When I'm searching for Monkey with the category Games, it comes up with nothing, but Monkey (island) is recorded in the database. :)

Cheers..
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

I'm *guessing* that your problem is with your MySQL query. I suggest that you try running the query on your database from the command prompt to see if there might be something wrong with it. Also, check out the use of mysql_error() to see if maybe your query is returning an error and thats why its not working. Hope that helps.
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

thanks, it works now, it was my sql query. I just had to remove the ORDER BY then it worked :) :) but how can I order the results if the this made the error ?

Thanks again :)
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

The order by is fairly simple...

Code: Select all

SELECT *
FROM `table`
WHERE field='data'
ORDER BY field DESC;
This will order the query by field in descending order. I suggest you read up on the ORDER BY clause in the MySQL manual. Hope that helps.
ampersand
Forum Commoner
Posts: 54
Joined: Thu Nov 28, 2002 2:00 am
Location: Norway

Post by ampersand »

Yes, i've used it in other queries, but this time it doesnt work, I used this:

Code: Select all

SELECT * FROM cd, sw, cat 
WHERE (sw.sw_title like "$search%") 
AND (sw.cat_id = "$cat" ) 
AND (sw.cd_id = cd.cd_id) ORDER BY (sw.sw_title "$radDirection")
But this query didnt work until I removed ORDER BY at the end. :?


Thanks..
R.A.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you tried removing the parenthesis from around the ORDER BY clause?

You also don't need to have lots of escaped double quotes in your SQL statement (in fact in SQL you shouldn't really have double quotes) because you can (and should) use single quotes instead:

Code: Select all

SELECT * FROM cd, sw, cat 
WHERE (sw.sw_title like '$search%') 
AND (sw.cat_id = '$cat' ) 
AND (sw.cd_id = cd.cd_id) ORDER BY sw.sw_title '$radDirection'
Mac
Post Reply