Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
timinator
Forum Newbie
Posts: 5 Joined: Thu Nov 06, 2003 11:13 am
Post
by timinator » Thu Nov 06, 2003 11:13 am
Hi, I have a search page and results page that is not working. The search form has 2 fields, a dropdown called "state" and a text box called "city" . When I choose a state or city I get an empty table. In the sql statement if I manually type in the state, "AL" for example it will work. Here is the code for the results page: thanks
Code: Select all
<?
$dbuser = "root";
$dbserver = "localhost";
$dbpass = "passwd";
$dbname = "NATFD";
//******** BEGIN LISTING THE CONTENTS OF company*********
//CONNECTION STRING
mysql_connect($dbserver, $dbuser, $dbpass)
or die ("UNABLE TO CONNECT TO DATABASE");
mysql_select_db($dbname)
or die ("UNABLE TO SELECT DATABASE");
if ($city=="")
$sql = ("SELECT * FROM company WHERE state='$state'");
elseif ($state=="")
$sql = ("SELECT * FROM company WHERE city='$city'");
else
$sql = ("SELECT * FROM company WHERE city='$city' AND state='$state'");
echo($sql);
$result = mysql_query($sql);
//if ($myrow = mysql_fetch_array($result))
$num_rows = mysql_num_rows($result);
print "<p><a href=javascript:history.back(1)><font size=1>BACK</font></a></p>";
print "<font size=3><strong>$num_rows Records found.<P>";
//Set up the Table
print "<table width=100% border=1>\n";
print "<tr>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>MLA</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>NAME</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>STREET</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>CITY</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>STATE</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>ZIP</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>PHONE</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>FAX</font></td>";
print "<td bgcolor=#CCCCCC><div align=center><font size=1>WEB SITE</font></td>";
print "</tr>";
//Get the data into the table
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td> <font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
?>
Edit: Added php tags for easier viewing. --JAM
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Thu Nov 06, 2003 11:26 am
viewtopic.php?t=511
Read that first, and see if it is the reason to your problem.
timinator
Forum Newbie
Posts: 5 Joined: Thu Nov 06, 2003 11:13 am
Post
by timinator » Thu Nov 06, 2003 11:43 am
I tried this but no luck....
$sql = ("SELECT * FROM company WHERE state= $_GET['$state']");
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Thu Nov 06, 2003 11:48 am
I was more thinking about this part(s) of the script:
There are also this issue:
Code: Select all
// bad
$sql = ("SELECT * FROM company WHERE state= $_GET['$state']");
// better
$sql = ("SELECT * FROM company WHERE state='$_GET[state]'");
timinator
Forum Newbie
Posts: 5 Joined: Thu Nov 06, 2003 11:13 am
Post
by timinator » Thu Nov 06, 2003 12:00 pm
Much better - Is it difficult to limit the numer of rows to say "50" and have next and previous links?
thanks
timinator
Forum Newbie
Posts: 5 Joined: Thu Nov 06, 2003 11:13 am
Post
by timinator » Thu Nov 06, 2003 1:40 pm
Thanks for the links - while I'm at it I will also need to some of the results columns to have links to detail pages.
thanks