PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
nou
Forum Newbie
Posts: 8 Joined: Tue Oct 25, 2011 3:21 am
Post
by nou » Tue Oct 25, 2011 3:29 am
Hello
This is the error:
Code: Select all
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\search.php on line 24
And this is the relative code:
Code: Select all
$zip=$_POST['zip']; //form text set to variable
$sql = "SELECT * FROM zip_codes WHERE zip_code='$zip'";
$result = mysql_query($sql);
if ($row = mysql_fetch_array($result)) {
echo $row['zip_code'] . " " . $row['city'];
echo "<br />";
}
Entire code;
welcome.php :
Code: Select all
<html>
<body>
<center>
<b>Zip code search!</b>
<form action="search.php" method="post">
<input type="text" name="zip"/><br>
<input type="submit" value="SEARCH"/>
</form>
</center>
</body>
</html>
search.php :
Code: Select all
<?php
$db_host = 'x';
$db_user = 'x';
$db_pass = 'x';
$db_name = 'x';
//set search text as variable
$zip=$_POST['zip'];
//echo text
echo $zip;
//kill script if null
if ($zip==NULL) {
die ("null" . mysql_error());
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
$sql = "SELECT * FROM zip_codes WHERE zip_code='$zip'";
$result = mysql_query($sql);
if ($row = mysql_fetch_array($result)) {
echo $row['zip_code'] . " " . $row['city'];
echo "<br />";
}
?>
I'm ripping my hair out trying to solve this. Any ideas? Thanks
Last edited by
nou on Tue Oct 25, 2011 3:45 am, edited 2 times in total.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Tue Oct 25, 2011 3:41 am
add var_dump($result) after your query to see what you're actually getting from mysql_query()
nou
Forum Newbie
Posts: 8 Joined: Tue Oct 25, 2011 3:21 am
Post
by nou » Tue Oct 25, 2011 3:47 am
I added the line you suggested, and now when I search a zipcode such as '94545' this is what I get:
Code: Select all
94545
bool(false)
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\search.php on line 26
Just making sure I added that right:
Code: Select all
$sql = "SELECT * FROM zip_codes WHERE zip_code='$zip'";
$result = mysql_query($sql);
var_dump($result);
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Tue Oct 25, 2011 3:58 am
ah, it appears you never connected to the MySQL server with mysql_connect()