| | |
mysql search and display data help....
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2006
Posts: 58
Reputation:
Solved Threads: 1
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?
Create a html form like this:
search.php
MySQL Syntax (Toggle Plain Text)
<form action="search.php" method="post"> <input type="text" name="search"><br> <input type="submit"> </form>
search.php
MySQL Syntax (Toggle Plain Text)
<?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>"; } } ?>
"If it is NOT source, it is NOT software."
-- NASA
-- NASA
MySQL Syntax (Toggle Plain Text)
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 3:07 pm.
"If it is NOT source, it is NOT software."
-- NASA
-- NASA
•
•
Join Date: Jan 2007
Posts: 1
Reputation:
Solved Threads: 0
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?:
in the previously posted code:
basically the same thing here, but without the search form.
would i need to change this line, or delete it?:
MySQL Syntax (Toggle Plain Text)
$search = "%" . $_POST["search"] . "%";
MySQL Syntax (Toggle Plain Text)
<?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>"; } } ?>
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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:
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:
MySQL Syntax (Toggle Plain Text)
<?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 } ?>
![]() |
Similar Threads
- display data in crystal report using vb (Visual Basic 4 / 5 / 6)
- how to display data from db (VB.NET)
- Display data in separate controls (SQL) (VB.NET)
- Horizontal display of data on Repeater control in asp.net (ASP.NET)
- How to send an action to servlet through hyperlink and display the data on to jsp. (JSP)
- PHP-MySQL retrieval or getting data (PHP)
- ASP display data in 3 col & rows using CSS layout (ASP)
Other Threads in the MySQL Forum
- Previous Thread: mysql error only on web page
- Next Thread: Need help on query!
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization






