mysql search help

I am a beginner in php, i have created a search form that i want to pull results from my mysql DB, problem is upon submitting it redirects to an HTTP 500 error. here are the scripts…

HTML search Form:

<form action="searching.php" method="GET">
Search For:
address:<input name="address" type="text" size="25"> <br>
direction:<select name="direction">
<option value="">&nbsp</option>
<option value="N">North</option>
<option value="NE">North East</option>
<option value="E">East</option>
<option value="SE">South East</option>
<option value="S">South</option>
<option value="SW">South West</option>
<option value="W">West</option>
<option value="NW">North West</option>
</select>  <br>
street:<input name="street" type="text" size="25"><br>
suffix:<input name="suffix" type="text" size="25"><br>
city:<input name="city" type="text" size="25"><br>
state:<input name="state" type="text" size="2"><br>
<input type=submit value="Search">


The searching.php file:


<?php $con = mysql_connect("xxxx","xxxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("metertrackers", $con);

$RESULT = MYSQL_QUERY "SELECT address, direction, street, suffix, city, state FROM dataentry WHERE 1 LIMIT 0, 30 “;
(’$_GET[address]’, ‘$_GET[direction]’, ‘$_GET[street]’, ‘$_GET[suffix]’,’$_GET[city]’, ‘$_GET[state]’)”;

if ($row = mysql_fetch_array($result) {
do {
print $row[“address”];
print (" “);
print $row[“direction”];
print (” “);
print $row[“street”];
print (” “);
print $row[“suffix”];
print (” “);
print $row[“city”];
print (” “);
print $row[“state”];
print (”

} while($row = mysql_fetch_array($result));

} else {print “No Results found, Try again later.”;}


If someone could look over it and see where the problem is, or if this is even gonna work. thank you, any advice is greatly appreciated.

( mysql login information was intentionally 'xxxx’ed)

Looks like you need to correct your call to mysql_query() function. Here is how your php code should look:

$result = mysql_query("SELECT address, direction, street, suffix, city, state FROM dataentry WHERE 1 LIMIT 0, 30 ");

Also eliminate this row entirely from your code:

(’$_GET[address]’, ‘$_GET[direction]’, ‘$_GET[street]’, ‘$_GET[suffix]’,’$_GET[city]’, ‘$_GET[state]’)";

The rest of your code looks fine.

Thanks for the quick response,

i changed the lines as you suggested but im still receiving the “cannot display page”. could the problem be in the actual html form where


Is the error still 500 (server error) or other? Form method GET should not be a problem here. Is PHP working properly on this server. You can test this by creating small file test.php and placing there single line:


<?php phpinfo() ?>


the error is just the http 500… cannot display page. I have php running, i have had no problem inserting into mysql through html forms and php.

After repetitive attempts i realized i could check the error logs on my apache server…

error was:
PHP Parse error: syntax error, unexpected ‘{’ in C:\Apache Software Foundation\Apache2.2\htdocs\searching.php on line 20, referer:

located the { on 20 and removed it, http 500 again, looked at log and showed:

PHP Parse error: syntax error, unexpected T_DO in C:\Apache Software Foundation\Apache2.2\htdocs\searching.php on line 22, referer:

I see the DO on line 22, but not the T_ …

Oh, this make sense. See in this line you’re missing one closing )
if ($row = mysql_fetch_array($result) {

Must be:
if ($row = mysql_fetch_array($result)) {

wow! one parenthese.thank you very much admin. it works now, well it shows results but not results i want. wonder if you could answer this as well…

now results show all the variables in the database for the fields. my goal was to input the address street etc and return results that match and show the entire field…

let me see if i can make it a little clearer.

the site i am putting together gathers information regarding residents water/electric/gas meters that ‘my’ company changes. the DB i created to test everything only has one table and the table has[size=4] 16 [/size][size=2]fields with specifics regarding the address and the old serial numbers new serial numbers a field for pictures (not working as of yet). im thinking for this to be practical i need to create multiple tables and link them using a unique id.[/size]

any advice would help.

the more i do this the more i feel like i should just contract this out.

willisww - don’t give up! :slight_smile:
Your task isn’t as difficult as it might seem to you.

You need to construct your WHERE sql clause, using parameters submitted from search form.
Here is how you do this.


<?php $where = ""; if($_GET["direction"] != "") $where.= ($where == ""?"":" and ")." direction='".addslashes(urldecode($_GET["direction"]))."'"; if($_GET["city"] != "") $where.= ($where == ""?"":" and ")." city='".addslashes(urldecode($_GET["city"]))."'"; if($_GET["street"] != "") $where.= ($where == ""?"":" and ")." street like '%".addslashes(urldecode($_GET["street"]))."%'"; $result = mysql_query("SELECT * FROM dataentry ".($where==""?"":(" WHERE ".$where))." LIMIT 0, 30"); ?>


Thank you coder! :smiley: that was what i needed,

also was not aware if i added ‘print $row’ and specify another field it will ‘print’ fields that correspond to that specified row.

your advice/help is greatly appreciated. i had seen the where clause before but did not realize what its function was.

i am curious about the addslashes and url decode, what are they? and how do they work?

sorry if i am asking to many questions, but php seems to be very versatile, especially when used in conjuction with mysql. i believe i read somehwere that they are called the ‘dynamic duo’ or something similar

Sponsor our Newsletter | Privacy Policy | Terms of Service