Jump to content

[SOLVED] PHP not pulling from phpMyAdmin starting at "0"...


ShootingBlanks

Recommended Posts

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!!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];
  }

 

Link to comment
Share on other sites

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!...

 

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.

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.