Jump to content

mysql_fetch_assoc misses id = 1


Nuv

Recommended Posts

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 -

 

state.PNG

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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> 

Link to comment
Share on other sites

Hey,

 

Sorry i didn't think  $counter = mysql_fetch_row($result);  could cause the problem  :o

 

Thus i just posted the code.I don't understand the reason but i will google it.

 

Need to become a good programmer ! :(

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.:'(

 

 

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.