Moving from an array to mysql

Hi everyone.
Currently, I am using an array to achieve my mission, and it works great, but the list is getting too big.
(I have over 300 entries and adding more each week)

Here is what I have so far:
[php]
$zips = array();

$zips[]= “33702”;
$zips[]= “33617”;
$zips[]= “33618”;
$zips[]= “33707”;
$zips[]= “33708”;
$zips[]= “33709”;

foreach($zips as $zip)

{
if($stZip == $zip)
{

$zipmatch = true;

}
}

if($zipmatch){

}else {
$locDeliv = $inpOpts[‘loc_unch_unav’];
}
[/php]

And what I am trying to do is not giving the same result:

[php]
$result =mysql_query(“SELECT 1 FROM zipcodes WHERE zipcodes = ‘$stzip’”);
if ($result && mysql_num_rows($result) > 0)
{
}
else
{
$locDeliv = $inpOpts[‘loc_unch_unav’];
}
[/php]

Any suggestions to how I can make this work?

Thank you!

Figured this out on my own.

Solution as follows:

[php]
$result = mysql_query(“SELECT zipcodes FROM zipcodes WHERE zipcodes=’$stZip’”);
$row = mysql_fetch_array( $result );
$zips = $row;
foreach($zips as $zip)
{
if($stZip == $zip)
{
$zipmatch = true;
}
}

if($zipmatch){

}else {
    $locDeliv = $inpOpts['loc_unch_unav'];
    
    				
}

[/php]

However, if someone sees a flaw, please point it out to me.

Thanks!

Your query should only return a result if there is a match. Iterating through the results with the foreach is unnecessary. I would do something like this:

[php]
$result = mysql_query(“SELECT zipcodes FROM zipcodes WHERE zipcodes=’$stZip’ limit 1”);
if(mysql_num_rows($result))
{
$row = mysql_fetch_array( $result );
// Whatever additional code you wish to execute if there was a match
}
else
{
$locDeliv = $inpOpts[‘loc_unch_unav’];
}
[/php]

Also, if you are using a numeric type (int, etc.) in your table for zipcode you may want to remove the apostrophes around the $stZip variable in your query.

Please let me know if this doesn’t work as expected.

Sponsor our Newsletter | Privacy Policy | Terms of Service