andersthorborg Posted November 5, 2010 Share Posted November 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217802-strange-result-of-mysql_fetch_array/ Share on other sites More sharing options...
fenway Posted November 7, 2010 Share Posted November 7, 2010 Sorry, I don't follow. Quote Link to comment https://forums.phpfreaks.com/topic/217802-strange-result-of-mysql_fetch_array/#findComment-1131385 Share on other sites More sharing options...
andersthorborg Posted November 8, 2010 Author Share Posted November 8, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/217802-strange-result-of-mysql_fetch_array/#findComment-1131752 Share on other sites More sharing options...
mikosiko Posted November 8, 2010 Share Posted November 8, 2010 without see the code that is populating the array (and ideally the row's data) is really difficult to give you an answer Quote Link to comment https://forums.phpfreaks.com/topic/217802-strange-result-of-mysql_fetch_array/#findComment-1131830 Share on other sites More sharing options...
jdavidbakr Posted November 11, 2010 Share Posted November 11, 2010 You don't happen to have an 'if($row['album_cover'] = 1)' statement between your fetch and your dump, do you? Quote Link to comment https://forums.phpfreaks.com/topic/217802-strange-result-of-mysql_fetch_array/#findComment-1133092 Share on other sites More sharing options...
DavidAM Posted November 11, 2010 Share Posted November 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217802-strange-result-of-mysql_fetch_array/#findComment-1133204 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.