ShootingBlanks Posted August 27, 2007 Share Posted August 27, 2007 I'm getting this weird issue where I'm trying to pull a set of records from a database table (that was created in phpMyAdmin), but the full record result isn't being pulled from the first record in the table (the "0" location). Instead, it starts at the second record (the "1" location). Here are examples of what I mean... I used the following code to populate a form drop-down menu with a list of category names from a database. It will populate all the names in the list EXCEPT for the first one, for some reason!: while($row = mysql_fetch_assoc($getCategories)){ echo '<option value="'.$row['cat_id'].'"'; if ($row['cat_id'] == $row_getDocument['cat_id']) { echo ' selected="selected"'; } echo '>'.$row['cat_name'].'</option>'; } Here's another example... I have a user table with the columns user_id, username, & pwd. I created a login screen. Every user can log in EXCEPT for the very first user in the table!... ...moreover, I added this line of code to the login screen: if ($row = mysql_fetch_row($LoginRS)) { $_SESSION['user_id'] = $row[0]; } That was supposed to capture the user_id (because the "0" location in that $row array would be the first column in the table - "user_id"). Then, I added this to the very top of the body of the page that the login screen directs to (just for testing purposes): <?php echo $_SESSION['user_id']; ?> What ends up happening is that it displays the "username". So, instead of $row[0] being passed to the $_SESSION variable like I wanted, it's passing $row[1]... Any ideas? This is kinda making me crazy! (I should also note that I am very much a PHP newbie, so this may be a very simple solution) Thanks!!! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 27, 2007 Share Posted August 27, 2007 Well, without any more code, im going to take a stab in the dark. Im going to assume that you are calling the mysql_fetch_row(or similar) function prior to your loop. If you take a look at the manual, you will notice that by calling any of these functions, you also move the internal array pointer. This basically means that next time you call the function, you are already on the second row. Hope that helps. Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted August 27, 2007 Author Share Posted August 27, 2007 Im going to assume that you are calling the mysql_fetch_row(or similar) function prior to your loop. Prior to what loop? I'm confused. Sorry again for my inexperience! ??? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 27, 2007 Share Posted August 27, 2007 This loop: while($row = mysql_fetch_assoc($getCategories)){ echo '<option value="'.$row['cat_id'].'"'; if ($row['cat_id'] == $row_getDocument['cat_id']) { echo ' selected="selected"'; } echo '>'.$row['cat_name'].'</option>'; } Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 You're using mysql_fetch_assoc in one place - use it all the time, not this: if ($row = mysql_fetch_row($LoginRS)) { $_SESSION['user_id'] = $row[0]; } Use: if ($row = mysql_fetch_assoc($LoginRS)) { $_SESSION['user_id'] = $row['user_id']; } Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted August 27, 2007 Author Share Posted August 27, 2007 jesirose - I tried this, and I got an error after logging in: $row = mysql_fetch_assoc($LoginRS); $_SESSION['user_id'] = $row['user_id']; GingerRobot - I think I may have confused you. The two examples in my original post were on totally separate PHP pages. So, when you said that I put the code prior to that loop, it actually wasn't even on the same page as that loop... ...my first block of code was from one page trying to populate a drop-down menu. And then the other two code blocks were from a different set of pages that were trying to capture a user_id field. Does that make more sense and help to troubleshoot my problem at all? Thanks so much fro writing back, you two!... Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted August 27, 2007 Author Share Posted August 27, 2007 Okay - regarding my post above, I just found out something else that's really strange about this... So, I said I was getting an error when using: $rowID = mysql_fetch_assoc($LoginRS); $_SESSION['user_id'] = $rowID['user_id']; So, I tried changing the variable on the 2nd line of the code to 'pwd': $rowID = mysql_fetch_assoc($LoginRS); $_SESSION['user_id'] = $rowID['pwd']; And that worked. I also changed it to 'username' and that worked too... ...SO, back to my original post - it seems that there's just something "wrong" with the first row and column in any table in my phpMyAdmin. I just don't get it! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 phpMyAdmin is a PHP-based tool which interacts with MySQL. It isn't actually anything to do with what you're doing. Is the row CALLED user_id? You have to change it to match your table, you can't just copy and paste everything and expect it to work. Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted August 27, 2007 Author Share Posted August 27, 2007 Is the row CALLED user_id? Yeah - that was definitely the very first thing I checked... ...hmmmm. ??? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 27, 2007 Share Posted August 27, 2007 Is there an entry for it? do: print_r($rowID); Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted August 28, 2007 Author Share Posted August 28, 2007 I almost feel stupid even admitting this... ...but I realized that in my SELECT query, I had been pulling all the fields from the "users" table EXCEPT for the "user_id". I'm a dope. Sorry for wasting all of your time, and I really do appreciate you all trying to help out!!! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 28, 2007 Share Posted August 28, 2007 Just do a SELECT * FROM if you're going to use all fields 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.