Dynadot โ€” .com Registration $8.99

PHP help needed - Again!

Spaceship Spaceship
Watch

electricbeat

Account Closed
Impact
0
PHP:
<?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.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
For the first part, after this:
PHP:
$search = $_POST["search"];
//assigns the value of the entered search term in to the variable "search"
Put this:
PHP:
if($search == NULL) {
     echo "The search field was left blank, please go back and try again.";
}
Then for the next part, after this:
PHP:
$result = mysql_query("SELECT * FROM users where username LIKE '%$search%'");
Place this:
PHP:
if(!mysql_fetch_array($result)) {
     echo "No results found.";
}
I think they should both work, not tested them though.
 
0
•••
thank you! The no results found part works :)

But, the thing where you enter no text doesnt
 
0
•••
Try changing that part to:

PHP:
if(!$search) {
     echo "The search field was left blank, please go back and try again.";
}
 
0
•••
electricbeat said:
PHP:
<?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:
 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:
if(strlen($search)<3) {

to specifiy a minimum search lenght as well (just the if bit, and 3 would be the minimum lenght)
 
0
•••
Thanks guys, it was sorted on another forum though, although I did use part of hairy's code, thanks :D
 
0
•••
No problem, glad to have helped :)
 
0
•••
"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?
 
0
•••
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back