Quick and hopefully simple PHP question

Associate
Joined
16 Aug 2004
Posts
268
I am querying a database and need to pull back some values and store them in the into an array so it looks like this:

Code:
$data = array(25,0,7,6,15,4,3,22,1);

This is what I have but it doesn't seem to be pulling the values in:

Code:
$query  = "SELECT * FROM items";
$result = mysql_query($query);
	
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	$data[] = $row['id'];
}

Can anyone point me in the right direction?
 
I would add some error checking to see if anything is failing.

Obviously you wouldn't want to die when you catch the exception in production code, but this is an example.

PHP:
$result = @mysql_query($query);
$data = array();
try {
	if (!$result) {
		throw new Exception(mysql_error());
	}
	if (mysql_num_rows($result) == 0) {
		throw new Exception('No results returned');
	}
	while ($row = mysql_fetch_assoc($result)) {
		$data[] = $row['id'];
	}
} catch (Exception $e) {
	die($e->getMesage());
}
 
Thanks for the reply, I tried using your above code but I must not have linked it in correctly as when I print the array I get the following:

Code:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => )

Which means it is querying my database as their are 6 results but it isn't showing them. Any ideas?
 
When you are trying to assign the variable to the array, you are infact telling the array how many values it should hold. So this is your code;

Code:
$data[] = $row['id'];

it infact should be;

Code:
$data[[B][COLOR="Orange"]Incrementing value HERE[/COLOR][/B]] = $row['id'];

That will then assign the data in id to the value of 0 in the array. To add it into your code you probably want to add in another variable outside of the while loop and then increment it for every loop e.g

Code:
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
    $data[$i] = $row['id'];
    $i++;
}

That will then assign the id of the current record to the correct value in the array.
 
$data[] to increment the array key by 1 and add a value is perfectly correct syntax though.

http://www.php.net/array - see the examples. Plus I have used this many countless times, and I like to pride myself on my PHP code being E_STRICT compliant.
 
Last edited:
When you are trying to assign the variable to the array, you are infact telling the array how many values it should hold. So this is your code;

Code:
$data[] = $row['id'];

it infact should be;

Code:
$data[[B][COLOR="Orange"]Incrementing value HERE[/COLOR][/B]] = $row['id'];

That will then assign the data in id to the value of 0 in the array. To add it into your code you probably want to add in another variable outside of the while loop and then increment it for every loop e.g

Code:
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
    $data[$i] = $row['id'];
    $i++;
}

That will then assign the id of the current record to the correct value in the array.

PHP doesn't require an index to be specified when appending an element to an array, so Moredhel's code is perfectly correct.
 
try this

$query = "SELECT * FROM items";
$result = mysql_query($query);
$data = array();

while($row = mysql_fetch_array($result))
{
array_push($data, $row['id']);
}
 
im new to php but have been dealing with array's recently so may be able to help.
"array_push" and "$data[] =" are both ok to use.

i'd say try this:

PHP:
var $data = Array();

$cid = mysql_connect($hostVar,$userVar,$passwordVar);
        if($cid) {
            $SQL='SELECT id FROM items';
            mysql_select_db($databaseVar, $cid) or die (mysql_error ());
            $retid = mysql_query($SQL, $cid);
            if ($retid) { 
                while ($row = mysql_fetch_array($retid)) {
                    array_push($this->items, $row["id"]);
                }
            }
       }
I have very similar code running on my test site and all is good!
Also, select * is bad practice. I know this is probably just for testing purposes but i thought i'd throw that in there too :)
 
Thanks for all the help everyone I have manged to get a bit further but am stuck again but it is more to do with Open Flash Charts.

I now have my array but when I'm sending the data over to create the graph my array has quotes around each value:

{"elements":[{"type":"bar_glass","colour":"#BF3B69","text":"Last year","font-size":12,"values":["25","19","15","6","13","11"]}],"title":{"text":"Changes - Last 5 Days"},"y_axis":{"min":0,"max":30,"steps":10}}

When it should look like:

{"elements":[{"type":"bar_glass","colour":"#BF3B69","text":"Last year","font-size":12,"values":[25,19,15,6,13,11]}],"title":{"text":"Changes - Last 5 Days"},"y_axis":{"min":0,"max":30,"steps":10}}

I'm pretty sure that it isn't php causing the problem as that is just the way it outputs an array and you can't really edit it.

EDIT: Fixed! found this if anyone ever has the same problem: http://forums.openflashchart.com/viewtopic.php?f=9&t=1738
 
Last edited:
Back
Top Bottom