Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 9th, 2003, 12:14 AM
shattered shattered is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 13 shattered User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Another question regarding search engine code

Hello all,
This is part of my code, if this looks familiar, it's taken from devshed.com:
PHP Code:
 $getresults mysql_query("SELECT * FROM Music_Library LIKE '%$search%'");

$resultsnumber mysql_numrows($getresults);

IF (
$resultsnumber == 0){
  PRINT 
"Your search returned no results. Try other keyword(s).";
}

ELSEIF (
$resultsnumber 0){
  PRINT 
"Your search returned $resultsnumber results<BR><BR>";
  for(
$count 0$count<$resultsnumber$count++){
  
$body mysql_result($getresults,$count);
  
//tighten up the results
  
$body2print substr($body0100);
  
$cnote $count+1;
  PRINT 
"$cnote. <a href=main.php>
  <i>$body2print...</i></a><BR>"
;
  }
}

?> 


1. I'm getting this error
"Warning: Supplied argument is not a valid MySQL result resource in /home/omis107/main.php on line 45"
I believe line 45 is "$resultsnumber = mysql_numrows($getresults);"

Can anyone tell me what the problem might be?

2. And in this line
$getresults = mysql_query("SELECT * FROM Music_Library LIKE '%$search%'");
What code should I write for $search if I want to perform a search of a string taken from the search engine text box?

3. And if you would look at this webpage , notice the alphabets at the top of the page. I'm hoping that if let's say I click on "A", it would bring up a list of data from my music_library table that starts with "A". How should I do that? Is there any reference that you can link me to?

Last edited by shattered : March 9th, 2003 at 01:40 AM.

Reply With Quote
  #2  
Old March 11th, 2003, 07:55 AM
trevHCS trevHCS is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 80 trevHCS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Re: Another question regarding search engine code

For the first question, try putting the following code just below:

$resultsnumber = mysql_numrows($getresults);


PHP Code:
 $mysql_error_no mysql_errno();
$mysql_error mysql_error();

if (!empty(
$mysql_error_no)) {

echo 
"MySql Said: $mysql_error_no : $mysql_error";
die; 


That'll output any error message from MySQL.


For question 2, do you mean how do you collect the contents of the search text box? If so, just with:

$search = '';
$search = $_POST['searchquery'];

First line makes sure the variable $search isn't tainted and second one collects the posted data from the form. "Searchquery" is the name of the form text box.

I would however recommend doing some checks on what they submit with commands like strip_tags() and escapeshellcmd() before sending it to the dbase. More info about those on http://www.php.net


For question 3:

I'm guessing on that page there should be links to the letters (unless it doesn't work on IE5.5). I'd guess you could link to each letter like:

searchscript.php?searchquery=a
searchscript.php?searchquery=b
etc..

Then you'd collect the data from the GET

$search = '';
$search = $_GET['searchquery'];

...and then in the SQL statement use something like:

LIKE '%$search'

...which if I remember means find anything beginning with $search, eg: a, b etc.


Another tip if you're having problems with SQL statements is to echo the SQL statement just to make sure there aren't any typos in there or problems with missing or invalid data.

Trev
--
http://www.aardvarktravel.net/

Reply With Quote
  #3  
Old March 13th, 2003, 11:24 AM
Morrigan's Avatar
Morrigan Morrigan is offline
Midnight Rider
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Quebec, Canada
Posts: 58 Morrigan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Re: Another question regarding search engine code

Quote:
Originally posted by shattered

1. I'm getting this error
"Warning: Supplied argument is not a valid MySQL result resource in /home/omis107/main.php on line 45"
I believe line 45 is "$resultsnumber = mysql_numrows($getresults);"

Can anyone tell me what the problem might be?

I thought it was mysql_num_rows(), not numrows()? (Note the underscore). Maybe both work. But I believe you have an error in your SQL statement.
SELECT * FROM Music_Library LIKE '%$search%' is wrong.
Try SELECT * FROM Music_Library WHERE field LIKE '%$search%' instead. "Field" is the table column in which you want to search. It could be a variable, if you want the user to choose to search in either artist name, album title, etc.


Quote:
3. And if you would look at this webpage , notice the alphabets at the top of the page. I'm hoping that if let's say I click on "A", it would bring up a list of data from my music_library table that starts with "A". How should I do that? Is there any reference that you can link me to?

You should pass the letter as a parameter to your page (like "list.php?letter=A", "list.php?letter=B", etc.), and the SQL would be something like "WHERE field = '$letter%'.

trevHCS gave some good advice, but he's wrong on one point, the wildcard (%) goes AFTER ($letter%, not %$letter) if you want to search for results that start with the letter (%$letter will find results that END with $letter). For an example, check out the link in my site, and go to "Browse bands by letter".
__________________
Encyclopaedia Metallum: The Metal Archives - the Ultimate Heavy Metal Archives! If it's not there, add it yourself.

Last edited by Morrigan : March 13th, 2003 at 11:26 AM.

Reply With Quote
  #4  
Old March 14th, 2003, 03:11 AM
trevHCS trevHCS is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 80 trevHCS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Re: Re: Another question regarding search engine code

Quote:
Originally posted by Morrigan
trevHCS gave some good advice, but he's wrong on one point, the wildcard (%) goes AFTER .....


Opps - good point! Should have checked some original code really before posting.

Trev

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Another question regarding search engine code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT