[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 07-08-2006, 11:12 AM   #1 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

electricbeat is an unknown quantity at this point


PHP help needed - again!

PHP Code:
<?php
//the config file should connect to mysql.
require("config.php");
    
//select database
mysql_select_db("users");

$search = $_POST["search"];
//assigns the value of the entered search term in to the variable "search"


$result = mysql_query("SELECT * FROM users where username LIKE '%$search%'");

//note "username" would be the field the search looks up, so if you searched for "Waffles" it would look up waffles username in the table users.

while($r=mysql_fetch_array($result))
//grabs all the content it found
{    
  
//then change this to match your mysql columns..this will be the information displayed. You could just have username.

   
$id=$r["id"];
   
$username=$r["username"];
   
$email=$r["email"];
   
$location=$r["location"];
   
$song1=$r["song1"];
   
$song2=$r["song2"];
   
$song3=$r["song3"];
   
$song4=$r["song4"];
   
$song5=$r["song5"];
   
$song6=$r["song6"];
   
$song7=$r["song7"];
   
$song8=$r["song8"];
   
$song9=$r["song9"];
   
$song10=$r["song10"];
   
$artist1=$r["artist1"];
   
$artist2=$r["artist2"];
   
$artist3=$r["artist3"];
   
$artist4=$r["artist4"];
   
$artist5=$r["artist5"];
   
$artist6=$r["artist6"];
   
$artist7=$r["artist7"];
   
$artist8=$r["artist8"];
   
$artist9=$r["artist9"];
   
$artist10=$r["artist10"];
//and so on
   
   // then just display the row
   
echo "Username: <i>$username</i><br />
Location: <i>$location</i><br />
Email: <i>$email</i><br><br>

<b>Music Interests</b><br />
1. $artist1 - <i>$song1</i><br />
2. $artist2 - <i>$song2</i><br />
3. $artist3 - <i>$song3</i><br />
4. $artist4 - <i>$song4</i><br />
5. $artist5 - <i>$song5</i><br />
6. $artist6 - <i>$song6</i><br />
7. $artist7 - <i>$song7</i><br />
8. $artist8 - <i>$song8</i><br />
9. $artist9 - <i>$song9</i><br />
10. $artist10 - <i>$song10</i><br />"
;
}
?>
So, I have a form that processes this file(search.php). If I type nothing in the form and push enter, I get all the results for all usernames. Instead I would prefer it to say ' You must enter text too search '. Also, when I search something that isn't in the database, and it's not found I don't have any error message. How could I make it so it returns ' No result found '.

All help appreciated.
electricbeat is offline  
Old 07-08-2006, 12:12 PM   #2 (permalink)
NamePros Regular
 
hairyfreak's Avatar
 
Join Date: Dec 2005
Location: Derbyshire, UK
Posts: 887
164.35 NP$ (Donate)

hairyfreak is just really nicehairyfreak is just really nicehairyfreak is just really nicehairyfreak is just really nice


For the first part, after this:
PHP Code:
$search = $_POST["search"];
//assigns the value of the entered search term in to the variable "search"
Put this:
PHP Code:
if($search == NULL) {
     echo
"The search field was left blank, please go back and try again.";
}
Then for the next part, after this:
PHP Code:
$result = mysql_query("SELECT * FROM users where username LIKE '%$search%'");
Place this:
PHP Code:
if(!mysql_fetch_array($result)) {
     echo
"No results found.";
}
I think they should both work, not tested them though.
hairyfreak is offline  
Old 07-08-2006, 12:29 PM   #3 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

electricbeat is an unknown quantity at this point


thank you! The no results found part works

But, the thing where you enter no text doesnt
electricbeat is offline  
Old 07-08-2006, 03:02 PM   #4 (permalink)
NamePros Regular
 
Flubber's Avatar
 
Join Date: Jun 2005
Location: UK
Posts: 691
25.95 NP$ (Donate)

Flubber is a splendid one to beholdFlubber is a splendid one to beholdFlubber is a splendid one to beholdFlubber is a splendid one to beholdFlubber is a splendid one to beholdFlubber is a splendid one to beholdFlubber is a splendid one to beholdFlubber is a splendid one to behold

Baby Health
Try changing that part to:

PHP Code:
if(!$search) {
     echo
"The search field was left blank, please go back and try again.";
}
Flubber is offline  
Old 07-08-2006, 03:34 PM   #5 (permalink)
NamePros Regular
 
-PS-'s Avatar
 
Join Date: Jun 2006
Location: Sydney
Posts: 251
5.95 NP$ (Donate)

-PS- will become famous soon enough-PS- will become famous soon enough


Quote:
Originally Posted by electricbeat
PHP Code:
<?php
//the config file should connect to mysql.
require("config.php");
    
//select database
mysql_select_db("users");

$search = $_POST["search"];
//assigns the value of the entered search term in to the variable "search"


$result = mysql_query("SELECT * FROM users where username LIKE '%$search%'");

//note "username" would be the field the search looks up, so if you searched for "Waffles" it would look up waffles username in the table users.

while($r=mysql_fetch_array($result))
//grabs all the content it found
{    
  
//then change this to match your mysql columns..this will be the information displayed. You could just have username.

   
$id=$r["id"];
   
$username=$r["username"];
   
$email=$r["email"];
   
$location=$r["location"];
   
$song1=$r["song1"];
   
$song2=$r["song2"];
   
$song3=$r["song3"];
   
$song4=$r["song4"];
   
$song5=$r["song5"];
   
$song6=$r["song6"];
   
$song7=$r["song7"];
   
$song8=$r["song8"];
   
$song9=$r["song9"];
   
$song10=$r["song10"];
   
$artist1=$r["artist1"];
   
$artist2=$r["artist2"];
   
$artist3=$r["artist3"];
   
$artist4=$r["artist4"];
   
$artist5=$r["artist5"];
   
$artist6=$r["artist6"];
   
$artist7=$r["artist7"];
   
$artist8=$r["artist8"];
   
$artist9=$r["artist9"];
   
$artist10=$r["artist10"];
//and so on
   
   // then just display the row
   
echo "Username: <i>$username</i><br />
Location: <i>$location</i><br />
Email: <i>$email</i><br><br>

<b>Music Interests</b><br />
1. $artist1 - <i>$song1</i><br />
2. $artist2 - <i>$song2</i><br />
3. $artist3 - <i>$song3</i><br />
4. $artist4 - <i>$song4</i><br />
5. $artist5 - <i>$song5</i><br />
6. $artist6 - <i>$song6</i><br />
7. $artist7 - <i>$song7</i><br />
8. $artist8 - <i>$song8</i><br />
9. $artist9 - <i>$song9</i><br />
10. $artist10 - <i>$song10</i><br />"
;
}
?>
So, I have a form that processes this file(search.php). If I type nothing in the form and push enter, I get all the results for all usernames. Instead I would prefer it to say ' You must enter text too search '. Also, when I search something that isn't in the database, and it's not found I don't have any error message. How could I make it so it returns ' No result found '.

All help appreciated.
PHP Code:
 if($search == NULL) {
     echo
"The search field was left blank, please go back and try again.";
}
that will work - just move the } to just before the ?>

ALso, you could change it to
PHP Code:
if(strlen($search)<3) {
to specifiy a minimum search lenght as well (just the if bit, and 3 would be the minimum lenght)
-PS- is offline  
Old 07-09-2006, 12:47 AM   #6 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

electricbeat is an unknown quantity at this point


Thanks guys, it was sorted on another forum though, although I did use part of hairy's code, thanks
electricbeat is offline  
Old 07-10-2006, 10:29 AM   #7 (permalink)
NamePros Regular
 
hairyfreak's Avatar
 
Join Date: Dec 2005
Location: Derbyshire, UK
Posts: 887
164.35 NP$ (Donate)

hairyfreak is just really nicehairyfreak is just really nicehairyfreak is just really nicehairyfreak is just really nice


No problem, glad to have helped
hairyfreak is offline  
Old 07-15-2006, 01:59 AM   #8 (permalink)
New Member
 
Join Date: Jul 2006
Posts: 20
0.00 NP$ (Donate)

Scolls is an unknown quantity at this point


"SELECT * FROM users where username LIKE '%$search%'" is a slow query, and searches through your entire table.

Why not rather have "SELECT * FROM users where username = '$search'" assuming the query is being built from the visitor's click on a username? Then also create an index on username for the first 2 or 3 characters?
Scolls is offline  
Old 07-15-2006, 02:11 AM   #9 (permalink)
NPQ's PA, Slave, and On Call Coder

Technical Services

 
Eric's Avatar
 
Join Date: Mar 2005
Posts: 4,545
0.71 NP$ (Donate)

Eric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond repute

Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse
If you hadn't noticed,
Quote:
electricbeat
Account Closed


Thread closed.
__________________
Eric is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 10:15 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85