Jump to content

array index changes


moosoft

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/221187-array-index-changes/
Share on other sites

If you read the documentation for mysql_fetch_array, you will learn that the second parameter is the result_type -

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/221187-array-index-changes/#findComment-1145248
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.