Jump to content

Moving from an Array to MySQL


twcast

Recommended Posts

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:

$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'];
}

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

 

$result =mysql_query("SELECT zipcodes FROM zipcodes WHERE zipcodes = '$stzip'");
if ($result && mysql_num_rows($result) > 0)
{
}
else
    {
   $locDeliv = $inpOpts['loc_unch_unav'];
    }

Link to comment
Share on other sites

Your MySQL code looks like it should function the same as the array code.

 

You really should be more specific, though. "Not giving the same result," doesn't give us much to work with.

 

Luckily, I think I see the problem. Your field name is the same as your database name. I'm guessing MySQL is having trouble discerning the two. try like this:

 

"SELECT zipcodes.zipcodes FROM zipcodes WHERE zipcodes = '$stzip'"

 

You really should change the field name, though. Just "zipcode" makes sense.

Link to comment
Share on other sites

Thank you

 

I have found the solution as follows:

 

$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){
   $locDeliv = $inpOpts['loc_ch_av'];
}else {
    $locDeliv = $inpOpts['loc_unch_unav'];
    
    				
}

 

However, if you see a flaw, please point it out to me as I am a newbie.

Link to comment
Share on other sites

I don't think that logic is correct. The actual matching of zipcodes should be taken care of in your SQL query. When you query for a specific zipcode in a list of zipcodes, the number of results (0 or 1) will indicate whether or not it matches. Like I said before, I don't think your query is working as you expect.

Link to comment
Share on other sites

I don't think that logic is correct. The actual matching of zipcodes should be taken care of in your SQL query. When you query for a specific zipcode in a list of zipcodes, the number of results (0 or 1) will indicate whether or not it matches. Like I said before, I don't think your query is working as you expect.

 

This is a site I am working on. I did not write this site, but there are variables in use the prohibit the use of echo.

 

They are using $buffer ="", and any echo inside of that throws an error. However, if I echo outside the buffer, I can echo to the top of the page.

Link to comment
Share on other sites

Variables have no affect on echo what so ever, or vice versa. There are literally thousands of examples on this site alone that you can indeed freely mix variables and echo, if not either would be quite useless.

What this means is that there's something else that causes the problem, and the actual error message tells you exactly what this is. You have to pay attention to the details, and every single one of them, when programming and not assume anything. If you don't know what the error message means, searching the web should inform you. In the case it doesn't, posting the error message here would go a long way in helping us to help you.

 

As for your logic: I'm afraid it's all over the place. As noted above the query should be returning one row only, or none, and by that fact alone you'd know whether or not it's a proper zip code. Incidentally, that's what your code does, but you've managed to do it in such an overly complex manner that people have actually missed this point.

You're actually checking the same zip code twice in this code, due to the way mysql_fetch_array () works. As such I strongly recommend that you read up in the PHP manual, and go through a couple of basic PHP tutorials. Your script could easily have been reduced to 5 lines, which would have made it both a lot easier to read and much more efficient.

 

PS: I also recommend that you read this article.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.