A Little Help Please

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
joez430
Forum Newbie
Posts: 4
Joined: Wed Feb 18, 2009 7:44 am

A Little Help Please

Post 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>
Oxidiser
Forum Newbie
Posts: 15
Joined: Wed Feb 18, 2009 5:00 am
Location: The Netherlands

Re: A Little Help Please

Post 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.
joez430
Forum Newbie
Posts: 4
Joined: Wed Feb 18, 2009 7:44 am

Re: A Little Help Please

Post 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
Oxidiser
Forum Newbie
Posts: 15
Joined: Wed Feb 18, 2009 5:00 am
Location: The Netherlands

Re: A Little Help Please

Post by Oxidiser »

Sory about that, I edited my post above to include brackets, {$row['state']}, try that.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: A Little Help Please

Post 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>";
 
Oxidiser
Forum Newbie
Posts: 15
Joined: Wed Feb 18, 2009 5:00 am
Location: The Netherlands

Re: A Little Help Please

Post 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.
joez430
Forum Newbie
Posts: 4
Joined: Wed Feb 18, 2009 7:44 am

Re: A Little Help Please

Post 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: A Little Help Please

Post by papa »

Oxidiser
Forum Newbie
Posts: 15
Joined: Wed Feb 18, 2009 5:00 am
Location: The Netherlands

Re: A Little Help Please

Post by Oxidiser »

Np.

Read this: http://nl3.php.net/manual/en/function.m ... string.php
And do a google search on mysql injections.
Oxidiser
Forum Newbie
Posts: 15
Joined: Wed Feb 18, 2009 5:00 am
Location: The Netherlands

Re: A Little Help Please

Post 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().
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: A Little Help Please

Post by papa »

Oops :)
joez430
Forum Newbie
Posts: 4
Joined: Wed Feb 18, 2009 7:44 am

Re: A Little Help Please

Post by joez430 »

Thanks all!
Post Reply