moosoft Posted December 10, 2010 Share Posted December 10, 2010 I tried searching for this but I didn't find anything. I have some php code that when loaded from a web page draws some numbers from a database and outputs. Should be no problem I thought The code involves a mysql query. This is an edited sample: $query = "SELECT `count` FROM `uc` WHERE `id` = 17"; $result = mysql_query($query, $dbh) or die(mysql_error()); $row = mysql_fetch_array($result, $dbh); Now after this I normally access $row as $row[0] and this works great most of the time. However about 10% of the time it returns an error: "Notice: Undefined offset: 0" My investigation using print_r($row); shows that sometimes it returns Array ( [0] => 619 ) and sometimes it returns Array ( [count] => 619 ) Why the inconsistency? How can I make it always use a numeric index? Quote Link to comment https://forums.phpfreaks.com/topic/221187-array-index-changes/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2010 Share Posted December 10, 2010 If you read the documentation for mysql_fetch_array, you will learn that the second parameter is the result_type - Quote result_type The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. Quote Link to comment https://forums.phpfreaks.com/topic/221187-array-index-changes/#findComment-1145248 Share on other sites More sharing options...
moosoft Posted December 10, 2010 Author Share Posted December 10, 2010 Thank you so much! I'll be sure to always specify that in the future. Quote Link to comment https://forums.phpfreaks.com/topic/221187-array-index-changes/#findComment-1145261 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.