Jump to content

Recommended Posts

I have been working on a CMS for a while using php and a mysql database and it has been working like a charm. Today i ran in to this strange issue:

 

I just implemented an image-album-table in the DB. This table has - amongst others - an "album_cover" field to store the ID of the currently selected image to be on the album cover. After fetching each row i wanted to retrieve the value using $row['album_id'], but I kept getting an error because the image with the ID did not exist (which I knew it did). To my surprise a print_r of 3 different $row-objects looks like this:

 

Array

(

    [0] => 1

    [id] => 1

    [1] => Test album

    [title] => Test album

    [2] =>

    [description] =>

    [3] => 97

    [album_cover] => 1

)

Array

(

    [0] => 2

    [id] => 2

    [1] => Test album  2

    [title] => Test album  2

    [2] => asdasd

    [description] => asdasd

    [3] => 95

    [album_cover] => 1

)

Array

(

    [0] => 3

    [id] => 3

    [1] => Another album

    [title] => Another album

    [2] => asdasd

    [description] => asdasd

    [3] => 98

    [album_cover] => 1

)

 

As expected for each field in the DB there is a numeric index and a key containing the same value. But as you can see for the album_cover, the numeric index and the key contains different values! How is this even possible?

 

For now I have fixed this by just using $row[3] instead of $row['album_cover'], but I am quite puzzled about how this is possible.

 

Any enlightment would be greatly appreciated.

 

All the best.

Link to comment
https://forums.phpfreaks.com/topic/217802-strange-result-of-mysql_fetch_array/
Share on other sites

Ok - thanks for letting me know.

 

This is a print_r of the result of mysql_fetch_array():

 

Array

=> 1

[id] => 1

    [1] => Test album

    [title] => Test album

    [2] =>

    [description] =>

    [3] => 97

    [album_cover] => 1

)

 

In short my problem is that in the about mentioned array - the value of $row['album_cover'] is 1 and the value of $row[3] is 97.

 

They should both be 97 according to the database (as they represent the same field in the database). This is the only case in my entire application where using $row['key'] results in a wrong value.

 

As mentioned for now I can work around it just by using the index instead of the key, but it sucks if eventually I want to add more fields to the table in the database, and I am just curious for any explanation for this strange behavior.

 

I hope this was a bit more clear.

 

Thanks!

It's possible you left out a comma in the SELECT statement:

 

SELECT id, title album_cover FROM ...

 

this select will put the data from the column named title into a field named album_cover.

 

However, without seeing some code, we can't really tell you what the problem is.

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.