Page 1 of 1

Perpetual error with MYSQL_FETCH_ARRAY; expects resource etc

Posted: Tue Oct 25, 2011 3:29 am
by nou
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

Re: Perpetual error with MYSQL_FETCH_ARRAY

Posted: Tue Oct 25, 2011 3:41 am
by Weirdan
add var_dump($result) after your query to see what you're actually getting from mysql_query()

Re: Perpetual error with MYSQL_FETCH_ARRAY; expects resource

Posted: Tue Oct 25, 2011 3:47 am
by nou
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);

Re: Perpetual error with MYSQL_FETCH_ARRAY; expects resource

Posted: Tue Oct 25, 2011 3:58 am
by Weirdan
ah, it appears you never connected to the MySQL server with mysql_connect()