User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 373,930 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,246 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 2799 | Replies: 5
Reply
Join Date: Aug 2006
Posts: 31
Reputation: desiguru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
desiguru desiguru is offline Offline
Light Poster

Help mysql search and display data help....

  #1  
Aug 2nd, 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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 142
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Junior Poster

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

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

search.php
<?php
  $search = "%" . $_POST["search"] . "%";

  mysql_connect ("localhost", "myuser", "mypassword");
  mysql_select_db ("mydatabase");

  $query = "SELECT mycolumn FROM mytable WHERE mycolumn LIKE '$search'";
  $result = mysql_query ($query);
  if ($result) {
    while ($row = mysql_fetch_array ($result)) {
      echo $row[0] . "<br>";
    }
  }
?>
"Premature optimization is the root of all evil."
Donald Knuth
Reply With Quote  
Join Date: Aug 2006
Posts: 31
Reputation: desiguru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
desiguru desiguru is offline Offline
Light Poster

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

  #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  
Join Date: Jul 2006
Location: Remunj
Posts: 142
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Junior Poster

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

  #4  
Aug 6th, 2006
if ($result) {
    while ($row = mysql_fetch_array ($result)) {
      foreach ($row as $key => $value) {
        echo "$key = $value<br>";
      }
      echo "<hr>";
    }
  }
Last edited by pritaeas : Aug 6th, 2006 at 2:07 pm.
"Premature optimization is the root of all evil."
Donald Knuth
Reply With Quote  
Join Date: Jan 2007
Posts: 1
Reputation: homienick is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
homienick homienick is offline Offline
Newbie Poster

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

  #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?:

 $search = "%" . $_POST["search"] . "%";
in the previously posted code:
<?php
  $search = "%" . $_POST["search"] . "%";

  mysql_connect ("localhost", "myuser", "mypassword");
  mysql_select_db ("mydatabase");

  $query = "SELECT mycolumn FROM mytable WHERE mycolumn LIKE '$search'";
  $result = mysql_query ($query);
  if ($result) {
    while ($row = mysql_fetch_array ($result)) {
      echo $row[0] . "<br>";
    }
  }
?>
Reply With Quote  
Join Date: Sep 2007
Posts: 1,054
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

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

  #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:
<?php
  $userid = $_POST["userid"];

  mysql_connect ("localhost", "myuser", "mypassword");
  mysql_select_db ("mydatabase");

  $query = "SELECT mycolumn FROM mytable WHERE mycolumn=$userid"
  $result = mysql_query ($query);
  if ($result) {
    //if this userid exists, do your coding here
  }else{
    //if this userid does not exist, do your coding here
  }
?>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 5:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC