...

View Full Version : mysql/php search page how to [ was mysql help ]



desiguru
08-01-2006, 07:05 AM
OK. SO this will a my very first project. It's very simple but since I do not know any mysql stuff.

I need to make a database with following three items.

1) City Name
2) Country in which city is located in.
3) City's Zip Code

That will be the MySQL part.

On the php section. I want to make a search.php page in which you can type any of the above and it will return a small table with all of the values.

For example.
If in my database. there were:
San Fransisco, California, 95001
San Jose, California, 95111
new york city, new york, 10111

and in the search.php's search box if I was to type 10111. The results will return me New York City, New Youk, 10111.


Contact me for more info. Thanks and have a peaceful day.

boeing747fp
08-01-2006, 07:16 AM
do you know how to use phpMyAdmin? if so, use that to create your mysql table.

as far as the php search page, you need a search form and probably would be a good idea to have a more limiting search form that has a "search by" feature... otherwise in the case of New York, New York, a query like you suggested would bring up all cities in the state New York as well as the city New York, New York.... so have a dropdown that options for Search By:
<select name='searchby'>
<option value='city'>City</option>
<option value='state'>State</option>
<option value='zipcode'>Zipcode</option>
</select>

and then have your text field
<input type="text" name="keyword">

then on the page your submit the form to, just run the following


<?php
$con = mysql_connect("localhost","username","password");
mysql_select_db("database_name",$con);
if($_POST['searchby'] == "city"){
$sql = myql_query("SELECT * FROM `table_name` WHERE `city` LIKE '%{$_POST['keyword']}%'") or die(mysql_error());
}elseif($_POST['searchby'] == "state"){
$sql = mysql_query("SELECT * FROM `table_name` WHERE `state` LIKE '{$_POST['keyword']}'") or die(mysql_error());
}elseif($_POST['searchby'] == "zipcode"){
$sql = mysql_query("SELECT * FROM `table_name` WHERE `zipcode` LIKE '{$_POST['keyword']}%'") or die(mysql_error());
}
while($row = mysql_fetch_array($sql)){
echo "$row[city], $row[state] $row[zipcode]<br />";
}
?>

guelphdad
08-01-2006, 01:21 PM
Contact me for more info. Thanks and have a peaceful day.

Please do not post a request for personal help to a problem you have posted to this forum. It is not acceptable practice. You have posted in a forum so you should expect to receive help in the forum. You can also post back any solution so that folks who follow into this thread with a similar concern can see how to approach the problem.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum