Page 1 of 1

A Little Help Please

Posted: Wed Feb 18, 2009 7:53 am
by joez430
I know I'll probably get flamed, but I would like to hash out my problems trying to get the results I'm looking for. I have gone through a few different tutorials, but I may not have found the right one yet. I have posted my code below in hopes a PHP/MYSQL expert can push me in the right direction. I'll need to tweak my select statement as well. What I'm trying to do is have a user enter their zip code and have it return a facility based on the zip. It would be ideal to do some sort of radius search, but that will be my next step. Currently, I'm trying to return the lab state,city,address, and phone number. How would I display ALL of the results that are given for a particular zip code?

Thank you,

Joe

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>

Code: Select all

 
        <?php
        $username="admin";
        $password="pass";
        $database="read";
        $zipcode = $_GET["zip"];
        $state = $_GET["state"];
 
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
$query = "SELECT state,address,city,phone,days,zip FROM labs WHERE zip = '$zipcode'";
 
 
//$db_connection = new mysqli("localhost", "admin", "pass", "read");
//$statement = $db_connection->prepare("SELECT zip FROM labs WHERE zip = ?");
//$statement->bind_param("s", $zipcode);
//$statement->execute();
 
        $result=mysql_query($query);
        $num=mysql_numrows($result);
 
        mysql_close();
 
       echo "<b><center>Database Output</center></b><br><br>";
 
$i=0;
while ($i < $num) {
 
$rs=mysql_result($result,$i,"zip");
 
 
echo "<b>$rs
$field2-name2</b><br>$field3-name<br>$field4-name<br>$field5-name<hr><br>";
 
$i++;
}
 
 
 
        ?>
 
</body>
</html>

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:14 am
by Oxidiser
Try:

Code: Select all

 
<?php
$username="admin";
$password="pass";
$database="read";
$zipcode = $_GET["zip"];
$state = $_GET["state"];
 
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
$query = "SELECT state,address,city,phone,days,zip FROM labs WHERE zip = '$zipcode'";
$result=mysql_query($query);
echo "<b><center>Database Output</center></b><br><br>";
while ($row = mysql_fetch_array($result)) {
  echo "<b>{$row['zip']} {$row['state']}</b><br>{$row['address']}<br>{$row['city']}<br>{$row['phone']}<br>{$row['days']}<hr><br>";
}
mysql_close();
 
And don't forget safety. You should escape $zipcode.

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:22 am
by joez430
Hi!

Thank you for the fast reply. I have changed the code and am now receiving an error.

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\lab\location_process.php on line 21

I'm also getting an error in Netbeans that says there is a syntax error on the

Code: Select all

echo "<b>$row['zip'] $row['state']</b><br>$row['address']<br>$row['city']<br>$row['phone']<br>$row['days']<hr><br>";
Thanks,

Joe

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:25 am
by Oxidiser
Sory about that, I edited my post above to include brackets, {$row['state']}, try that.

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:27 am
by susrisha

Code: Select all

 
[color=#FF0000]echo "<b>$row['zip'] $row['state']</b><br>$row['address']<br>$row['city']<br>$row['phone']<br>$row['days']<hr><br>";[/color]
 
echo "<b>".$row['zip']." ". $row['state']."</b><br>".$row['address']."<br>".$row['city']."<br>".$row['phone']."<br>".$row['days']."<hr><br>";
 

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:28 am
by Oxidiser
susrisha wrote:

Code: Select all

 
[color=#FF0000]echo "<b>$row['zip'] $row['state']</b><br>$row['address']<br>$row['city']<br>$row['phone']<br>$row['days']<hr><br>";[/color]
 
echo "<b>".$row['zip']." ". $row['state']."</b><br>".$row['address']."<br>".$row['city']."<br>".$row['phone']."<br>".$row['days']."<hr><br>";
 
Yes, that will work fine too.

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:29 am
by joez430
Oxidiser,

Your code works perfect! 8) . Thank you very much! What do you mean I should escape the variable? What would be a good Google search?

Thank you,

Joe

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:30 am
by papa

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:31 am
by Oxidiser
Np.

Read this: http://nl3.php.net/manual/en/function.m ... string.php
And do a google search on mysql injections.

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:31 am
by Oxidiser
Version Description
5.3.0 This function now throws an E_DEPRECATED notice.
4.3.0 This function became deprecated, do not use this function. Instead, use mysql_real_escape_string().

Re: A Little Help Please

Posted: Wed Feb 18, 2009 8:34 am
by papa
Oops :)

Re: A Little Help Please

Posted: Wed Feb 18, 2009 9:03 am
by joez430
Thanks all!