Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass parameter

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
hi, i am trying to select recordset from mysql based on parameter passed from html (vzip is view)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>ZIP Code</title>
</head>
<body>
<form action="zip.php" method="post">
ZIP Code: <input type="text" name="zip">
<p><input type="Submit">
<br>
<br>
</p>
</form>
</body>
</html>


zip.php

<?php
$username="root";
$password="jp";
$database="db2";

$zip=$_POST['zip'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM vzip WHERE zip='$zip' ";
mysql_query($query);

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$zip=mysql_result($result,$i,"zip");
$state=mysql_result($result,$i,"state");
$city=mysql_result($result,$i,"city");
$county=mysql_result($result,$i,"ziplist_county");
$snowload=mysql_result($result,$i,"s");

echo "<br>ZIP: $zip<br>State: $state<br>City: $city<br>County: $county<br>Snow load: $snowload<br><hr><br>";

$i++;
}

?>

but instead of expected result

ZIP CITY STATE ZIPLIST_COUNTY S
54501 Rhinelander WI Oneida 60


i am getting:

Database Output

"; $zip=mysql_result($result,"zip"); $state=mysql_result($result,"state"); $city=mysql_result($result,"city"); $county=mysql_result($result,"ziplist_county"); $snowload=mysql_result($result,"s"); echo "
ZIP: $zip
State: $state
City: $city
County: $county
Snow load: $snowload

"; ?>

can anyone help me, please? this is my first project with php/mysql.
 
First off I'd remove the @ from the mysql_select_db, if you get an error you'll never see it and secondly why are you using mysql_result rather than one of the mysql_fetch functions?
 
thanks, i removed it so it looks now like this:

<?php
$username="root";
$password="jp";
$database="db2";

$zip=$_POST['zip'];

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM vzip WHERE zip='$zip'";
echo $query
mysql_query($query);

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$zip=mysql_result($result,"zip");
$state=mysql_result($result,"state");
$city=mysql_result($result,"city");
$county=mysql_result($result,"ziplist_county");
$snowload=mysql_result($result,"s");

echo "<br>ZIP: $zip<br>State: $state<br>City: $city<br>County: $county<br>Snow load: $snowload<br><hr><br>";

?>


but still not getting right results, my screen says:

Database Output


"; $zip=mysql_result($result,"zip"); $state=mysql_result($result,"state"); $city=mysql_result($result,"city"); $county=mysql_result($result,"ziplist_county"); $snowload=mysql_result($result,"s"); echo "
ZIP: $zip
State: $state
City: $city
County: $county
Snow load: $snowload

"; ?>
 
i did more updates:

<?php

$username="root";
$password="jp";
$database="db2";

$zip=$_POST['zip'];

$con =mysql_connect(localhost,$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM vzip WHERE zip='$zip'";
mysql_query($query, $con);

$zip=mysql_result($result,0,"zip");
$state=mysql_result($result,0,"state");
$city=mysql_result($result,0,"city");
$county=mysql_result($result,0,"ziplist_county");
$snowload=mysql_result($result,0,"s");

mysql_close($con);

echo "<br>ZIP: $zip<br>State: $state<br>City: $city<br>County: $county<br>Snow load: $snowload<br><hr><br>";

?>


but still no result :-(
 
Well mysql_result() takes a parameter that is supposed to be the the handle to the result set from the query. $result, however your variable $result does not get set anywhere.

Try this:

Code:
[red]$result=[/red]mysql_query($query,$con);

I'm more surprised however that you aren't getting any errors from that.


You could of course use the mysql_fetch array method as per Ingresman's suggestion:

Code:
$query = "SELECT * FROM vzip WHERE zip='$zip'";
$result=mysql_query($query, $con);
while ($row=mysql_fetch_array($result)
{

$zip=$row['zip'];
$state=$row['state'];
$city=$row['city']
$county=$row['county'];
$snowload=$row['county'];
echo "<br>ZIP: $zip<br>State: $state<br>City: $city<br>County: $county<br>Snow load: $snowload<br><hr><br>";
}
mysql_close($con);

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I wonder why php didn't report that $result is not a valid handle?
 
i am sorry, it was my mistake how i used it; i am running XAMPP. the major problem wasn;t php.

my zip.php worked fine if i called it directly -
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Wines</title>
</head>
<body>
<pre>
<?php

$zip=$_POST['zip'];

$connection = mysql_connect("localhost","root","jp");

mysql_select_db("db2", $connection);

$result = mysql_query ("SELECT * FROM
vzip WHERE zip='54501'", $connection);

//$result = mysql_query ("SELECT * FROM
// vzip WHERE zip='$zip'", $connection);

while ($row = mysql_fetch_array($result, MYSQL_NUM))
{

foreach ($row as $attribute)
print "{$attribute} ";

print "\n";
}
?>
</pre>
</body>
</html>

when open my zip.html - file:///C:/xampp/htdocs/xampp/zip.html - it didn't work

i have to call it using localhost - it and it works fine.

my apologies and thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top