mmckimson1 Posted October 29, 2014 Share Posted October 29, 2014 First of all, I apologize for my "newbieness" in advance. I've got a form processing script that is working fine except for one section of code, snippets of which is shown below: //default values $formResponse = "How are you really doing? Use this to find out!"; $imgSource = "blue.png"; If ($country == "United States") { //US zip code query $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1"; mysql_select_db('db'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { $formResponse = "It looks like you entered an invalid zip code. Please try again!"; $imgSource = "yellow.png"; } else { $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1"; mysql_select_db('db'); $retval = mysql_query( $sql, $conn ); while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { $location = $row['location']; $revParData = $row['revParData']; } } What I thought would happen is this: If the zip code entered by the end user does not exist in the table, the $formResponse and $imgSource values would be the "... invalid zip code" and "yellow.png" values. If a valid zip code is entered, it completes the second query. In fact, it works fine if a zip code that exists in the table is entered, but shows the default values for $formResponse and $imgSource if a zip code that doesn't exist in the database is entered. I can't figure out why it is doing this... if anyone can point out my coding error, it would be appreciated. Cheers! Mike Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 29, 2014 Share Posted October 29, 2014 Horribly formatted code post. Let me look at it for a bit. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted October 29, 2014 Solution Share Posted October 29, 2014 You are working on the incorrect assumption that returning no results will result in query failure. No results is a perfectly valid result. Check the number of rows returned instead to see if any records were found. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 29, 2014 Share Posted October 29, 2014 Wow! This is a pretty incomplete section of code! Here it is cleaned up a bit. //default values $formResponse = "How are you really doing? Use this to find out!"; $imgSource = "blue.png"; If ($country == "United States") { //US zip code query $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1"; mysql_select_db('db'); $retval = mysql_query( $sql, $conn ); if(!$retval ) { $formResponse = "It looks like you entered an invalid zip code. Please try again!"; $imgSource = "yellow.png"; } else { $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode' LIMIT 1"; mysql_select_db('db'); $retval = mysql_query( $sql, $conn ); while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { $location = $row['location']; $revParData = $row['revParData']; } } So incomplete. As Barand said - no results is not the same as "!$results)". Going thru this code I see that you check to see if you are in the US and you do a query for a single zip code record. Incorrectly you then output the 'invalid code' message if the query fails. Skipping that you then re-run the exact same query if the query succeeded and once again output what you think is going to be a result. Actually you need to not only check if the query ran (which you do) but also if it returned a result/row (which you don't. Why you run the query twice - who know? Furthermore - if you use the php manual you must have noticed that the MySQL_* functions are deprecated. Use At Your Own Risk. But since you are set in your ways here is how I would re-write it: $formResponse = "How are you really doing? Use this to find out!"; $imgSource = "blue.png"; mysql_select_db('db'); If ($country == "United States") { $sql = "SELECT location, revParData FROM zipData WHERE zipCode = '$zipCode'"; $retval = mysql_query( $sql, $conn ); if(!$retval ) { $formResponse = "Error running query - ".mysql_error(); $imgSource = "yellow.png"; } if ( mysql_num_rows() == 0) { $formResponse = "It looks like you entered an invalid zip code. Please try again!"; $imgSource = "yellow.png"; } else { $row = mysql_fetch_array($retval, MYSQL_ASSOC)) $location = $row['location']; $revParData = $row['revParData']; } ... No dupe query, no dupe select db, proper handling of all results. Now stop using MySQL_* 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.