mysql search and display data help....

Reply

Join Date: Aug 2006
Posts: 58
Reputation: desiguru is an unknown quantity at this point 
Solved Threads: 1
desiguru desiguru is offline Offline
Junior Poster in Training

mysql search and display data help....

 
0
  #1
Aug 3rd, 2006
First of all this is a crazy thing I have just done.. so please sit back and read the whole post.............. Today I finished my math final and decided to learn a new programming language..... MYSQL. So I was able make my way to http://www.freewebmasterhelp.com/tutorials/phpmysql using Google I followed most of the tutorial and got the database setup... up until part 4; Everything works great and now I am just curious that how can I make a small page called search.php in which I can make a small search box. and have only one result display from the MySQL?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 831
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: mysql search and display data help....

 
0
  #2
Aug 3rd, 2006
Create a html form like this:
  1. <form action="search.php" method="post">
  2. <input type="text" name="search"><br>
  3. <input type="submit">
  4. </form>

search.php
  1. <?php
  2. $search = "%" . $_POST["search"] . "%";
  3.  
  4. mysql_connect ("localhost", "myuser", "mypassword");
  5. mysql_select_db ("mydatabase");
  6.  
  7. $query = "SELECT mycolumn FROM mytable WHERE mycolumn LIKE '$search'";
  8. $result = mysql_query ($query);
  9. if ($result) {
  10. while ($row = mysql_fetch_array ($result)) {
  11. echo $row[0] . "<br>";
  12. }
  13. }
  14. ?>
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 58
Reputation: desiguru is an unknown quantity at this point 
Solved Threads: 1
desiguru desiguru is offline Offline
Junior Poster in Training

Re: mysql search and display data help....

 
0
  #3
Aug 3rd, 2006
I got it to work but it displays only one column instead of showing all of the columns from the database.

Is there anyway I can show all of the columns?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 831
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: mysql search and display data help....

 
0
  #4
Aug 6th, 2006
  1. if ($result) {
  2. while ($row = mysql_fetch_array ($result)) {
  3. foreach ($row as $key => $value) {
  4. echo "$key = $value<br>";
  5. }
  6. echo "<hr>";
  7. }
  8. }
Last edited by pritaeas; Aug 6th, 2006 at 3:07 pm.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 1
Reputation: homienick is an unknown quantity at this point 
Solved Threads: 0
homienick homienick is offline Offline
Newbie Poster

Re: mysql search and display data help....

 
0
  #5
Oct 24th, 2007
I'm trying to do just this, but when a persons profile loads, it grabs their user_id as the page is loading, then searches the database for that user_id, and displays the rows it found that contained that user_id.

basically the same thing here, but without the search form.

would i need to change this line, or delete it?:

  1. $search = "%" . $_POST["search"] . "%";
in the previously posted code:
  1. <?php
  2. $search = "%" . $_POST["search"] . "%";
  3.  
  4. mysql_connect ("localhost", "myuser", "mypassword");
  5. mysql_select_db ("mydatabase");
  6.  
  7. $query = "SELECT mycolumn FROM mytable WHERE mycolumn LIKE '$search'";
  8. $result = mysql_query ($query);
  9. if ($result) {
  10. while ($row = mysql_fetch_array ($result)) {
  11. echo $row[0] . "<br>";
  12. }
  13. }
  14. ?>
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: mysql search and display data help....

 
0
  #6
Oct 26th, 2007
For all:
all the "%" do is tell MySQL it is a wild card. So "am" with "%am%" pulls up anything with any number of letters before the combo "am" and any number of letters after:

name - n am e
tame - t am e
madam - mad am
ambulance - am bulance

understand? So if you're searching the database for a userid, you do not want the LIKE scenario, you want the normal "userid" not, "%userid%".
Furthermore, "%am" will pull only words beginning with anything but ending with am, like "sam", and same as the other way "am%" pulls words like "ambuleance".

For your search page, if you only want one result, add this line to your SQL query: "LIMIT 1"
This way it will only return one result and not waste memory holding the other 200 results that are being unused.

For homienick
So, following the code above as I am not a PHP programmer, use it like below to pull your users info:
  1. <?php
  2. $userid = $_POST["userid"];
  3.  
  4. mysql_connect ("localhost", "myuser", "mypassword");
  5. mysql_select_db ("mydatabase");
  6.  
  7. $query = "SELECT mycolumn FROM mytable WHERE mycolumn=$userid"
  8. $result = mysql_query ($query);
  9. if ($result) {
  10. //if this userid EXISTS, DO your coding here
  11. }ELSE{
  12. //if this userid does NOT exist, DO your coding here
  13. }
  14. ?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC