Nuv Posted March 9, 2011 Share Posted March 9, 2011 Following is the simple code to print name of states. However it doesn't print "Alabama" or in short it misses id=1.Can someone please point out my mistake or tell me why won't it print Alabama and ignoring id = 1 ? While debugging i found out it starts from id= 2. Code - <?php $sql = "SELECT * FROM state"; $result = mysql_query($sql); ?> <SELECT NAME="state"> <OPTION value="">Select State</option> <?php while($arrayRow = mysql_fetch_assoc($result)) { $strA = $arrayRow["id"]; $strB = $arrayRow["state"]; echo "<option value=\"$strB\">$strB</option>\n"; }?> </SELECT> Sql table image - Quote Link to comment https://forums.phpfreaks.com/topic/230096-mysql_fetch_assoc-misses-id-1/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 9, 2011 Share Posted March 9, 2011 A) What does a 'view source' in your browser show? B) What's your actual php code, because the symptom is usually due to someone putting in a mysql_fetch_assoc() statement somewhere between their mysql_query() statement and the start of their while(){} loop? Quote Link to comment https://forums.phpfreaks.com/topic/230096-mysql_fetch_assoc-misses-id-1/#findComment-1185010 Share on other sites More sharing options...
Nuv Posted March 9, 2011 Author Share Posted March 9, 2011 A) What does a 'view source' in your browser show? <SELECT NAME="state"> <OPTION value="">Select State</option> <option value="Alaska">Alaska</option> <option value="Arizona">Arizona</option> <option value="Arkansas">Arkansas</option> <option value="California">California</option> <option value="Colorado">Colorado</option> <option value="Connecticut">Connecticut</option> <option value="Delaware">Delaware</option> <option value="District of Columbia">District of Columbia</option> <option value="Florida">Florida</option> <option value="Georgia">Georgia</option> <option value="Hawaii">Hawaii</option> <option value="Idaho">Idaho</option> <option value="Illinois">Illinois</option> <option value="Indiana">Indiana</option> <option value="Iowa">Iowa</option> <option value="Kansas">Kansas</option> <option value="Kentucky">Kentucky</option> <option value="Louisiana">Louisiana</option> <option value="Maine">Maine</option> <option value="Maryland">Maryland</option> <option value="Massachusetts">Massachusetts</option> <option value="Michigan">Michigan</option> <option value="Minnesota">Minnesota</option> <option value="Mississippi">Mississippi</option> <option value="Missouri">Missouri</option> <option value="Montana">Montana</option> <option value="Nebraska">Nebraska</option> <option value="Nevada">Nevada</option> <option value="New Hampshire">New Hampshire</option> <option value="New Jersey">New Jersey</option> <option value="New Mexico">New Mexico</option> <option value="New York">New York</option> <option value="North Carolina">North Carolina</option> <option value="North Dakota">North Dakota</option> <option value="Ohio">Ohio</option> <option value="Oklahoma">Oklahoma</option> <option value="Oregon">Oregon</option> <option value="Pennsylvania">Pennsylvania</option> <option value="Rhode Island">Rhode Island</option> <option value="South Carolina">South Carolina</option> <option value="South Dakota">South Dakota</option> <option value="Tennessee">Tennessee</option> <option value="Texas">Texas</option> <option value="Utah">Utah</option> <option value="Vermont">Vermont</option> <option value="Virginia">Virginia</option> <option value="Washington">Washington</option> <option value="West Virginia">West Virginia</option> <option value="Wisconsin">Wisconsin</option> <option value="Wyoming">Wyoming</option> </SELECT> B) What's your actual php code, because the symptom is usually due to someone putting in a mysql_fetch_assoc() statement somewhere between their mysql_query() statement and the start of their while(){} loop? <?php $sql = "SELECT * FROM state"; $result = mysql_query($sql); // $statarray = mysql_fetch_array($result); $counter = mysql_fetch_row($result); // echo $statarray[0]; ?> <p><strong>Please Use the following form to search</a> </strong></p> <form action="" method="post" id="sendemail"> <ol> <li> <label for="name">Florist Name</label> <input id="name" name="name" class="text" /> </li> <li> <label for="address">Address</label> <input id="address" name="address" class="text" /> </li> <li> <label for="city">City</label> <input id="city" name="city" class="text" /> </li> <li> <label for="state">State</label> <SELECT NAME="state"> <OPTION value="">Select State</option> <?php while($arrayRow = mysql_fetch_array($result)) { $strA = $arrayRow["id"]; $strB = $arrayRow["state"]; echo "<option value=\"$strB\">$strB</option>\n"; }?> </SELECT> </li> <li> <label for="zipcode">Pincode/Zipcode</label> <input id="zipcode" name="zipcode" class="text" /> </li> <li> <input type="image" name="submit" id="imageField" src="images/submit.gif" class="send" /> <div class="clr"></div> </li> </ol> </form> Quote Link to comment https://forums.phpfreaks.com/topic/230096-mysql_fetch_assoc-misses-id-1/#findComment-1185030 Share on other sites More sharing options...
PFMaBiSmAd Posted March 9, 2011 Share Posted March 9, 2011 @Nuv, if you are not even trying to solve the problems in your own code, especially if someone has posted the reason why your code is not working, you are not going to succeed as a programmer. Quote Link to comment https://forums.phpfreaks.com/topic/230096-mysql_fetch_assoc-misses-id-1/#findComment-1185043 Share on other sites More sharing options...
Nuv Posted March 9, 2011 Author Share Posted March 9, 2011 Hey, Sorry i didn't think $counter = mysql_fetch_row($result); could cause the problem Thus i just posted the code.I don't understand the reason but i will google it. Need to become a good programmer ! Quote Link to comment https://forums.phpfreaks.com/topic/230096-mysql_fetch_assoc-misses-id-1/#findComment-1185074 Share on other sites More sharing options...
litebearer Posted March 9, 2011 Share Posted March 9, 2011 Well this... $counter = mysql_fetch_row($result); says - put the information from the first row into a variable named $counter at that point, it now moves to the second row of information being returned you either need to remove that line OR reset the point back to the first row being returned Quote Link to comment https://forums.phpfreaks.com/topic/230096-mysql_fetch_assoc-misses-id-1/#findComment-1185080 Share on other sites More sharing options...
Nuv Posted March 9, 2011 Author Share Posted March 9, 2011 Well this... $counter = mysql_fetch_row($result); says - put the information from the first row into a variable named $counter at that point, it now moves to the second row of information being returned you either need to remove that line OR reset the point back to the first row being returned Thanks a lot.Now i understand . Silly me.:'( Quote Link to comment https://forums.phpfreaks.com/topic/230096-mysql_fetch_assoc-misses-id-1/#findComment-1185093 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.