Jump to content

[SOLVED] setting array key to 1 instead of 0 with mysql_fetch_array


Optimo

Recommended Posts

as the topic states i have a mysql statement that is selecting many records from a DB and then im using them in an array later and in a for loop where $x changes thus changing the array data but i want the array to start form key 1 instead of key 0

 

i know theirs an easy way to do it as with other languages but im pretty new to arrays and ive not been able to find it on php.net

 

here and example of how my code works:

 

$sql = mysql_query("SELECT * FROM __________") or die(mysql_error());

while($row = mysql_fetch_array($sql))
{$unit_data_array[] = $row;}

for($x = 1; $x < 17; $x++)
{		
	$unit_data = $unit_data_array[$x];
	$unit_name = $unit_data[name];
	$unit_crew = $unit_data[crew];
	$unit_type = $unit_data[type];
	$unit_money = $unit_data[money];
	$unit_deterium = $unit_data[deterium];
                ........
        }

 

 

and $x = 1 is displaying data from the 2nd record in the array and i want it to show the 1st record.

 

any help would be great thanks

You could always add element 0 first

 

$sql = mysql_query("SELECT * FROM __________") or die(mysql_error());

$unit_data_array[0] = '';

while($row = mysql_fetch_array($sql))
{$unit_data_array[] = $row;}

BTW, if you are storing each row in an array like that I'd use either

mysql_fetch_row()

mysql_fetch_assoc()

 

Using mysql_fetch_array() without optional second argument stores each column value twice.

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.