NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP help needed - again!

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

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 07-08-2006, 12:12 PM THREAD STARTER               #1 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



PHP help needed - again!


PHP Code:
<?php
????: NamePros.com http://www.namepros.com/programming/214848-php-help-needed-again.html
????: NamePros.com http://www.namepros.com/showthread.php?t=214848
//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, 01:12 PM   #2 (permalink)
NamePros Regular
 
hairyfreak's Avatar
Join Date: Dec 2005
Location: Derbyshire, UK
Posts: 887
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.";
????: NamePros.com http://www.namepros.com/showthread.php?t=214848

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, 01:29 PM THREAD STARTER               #3 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
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, 04:02 PM   #4 (permalink)
NamePros Regular
 
Flubber's Avatar
Join Date: Jun 2005
Location: UK
Posts: 688
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) {
????: NamePros.com http://www.namepros.com/showthread.php?t=214848
     echo 
"The search field was left blank, please go back and try again.";

Flubber is offline  
Old 07-08-2006, 04:34 PM   #5 (permalink)
NamePros Regular
 
-PS-'s Avatar
Join Date: Jun 2006
Location: Sydney
Posts: 251
-PS- will become famous soon enough-PS- will become famous soon enough
 



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"];
????: NamePros.com http://www.namepros.com/showthread.php?t=214848
   
$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 />
????: NamePros.com http://www.namepros.com/showthread.php?t=214848
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, 01:47 AM THREAD STARTER               #6 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
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, 11:29 AM   #7 (permalink)
NamePros Regular
 
hairyfreak's Avatar
Join Date: Dec 2005
Location: Derbyshire, UK
Posts: 887
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, 02:59 AM   #8 (permalink)
New Member
Join Date: Jul 2006
Posts: 20
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, 03:11 AM   #9 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:45 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger