twcast Posted September 12, 2012 Share Posted September 12, 2012 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']; } Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 1. Put your query in a variable and echo it so we can see it. 2. Does the query work if you run it directly in mysql/phpmyadmin? 3. Did you store the zipcodes as int or varchar? Quote Link to comment Share on other sites More sharing options...
twcast Posted September 12, 2012 Author Share Posted September 12, 2012 I cannot use echo, I will get an error. This code matches the one I use to input zipcodes in the database, and alert me if a duplicate entry already exists. I used Varchar in the database. Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 12, 2012 Share Posted September 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
twcast Posted September 12, 2012 Author Share Posted September 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 I think the fact that you can't echo a simple string for debugging is the biggest problem. Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 12, 2012 Share Posted September 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
twcast Posted September 12, 2012 Author Share Posted September 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 12, 2012 Share Posted September 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.