Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Arrays

Post by sandy1028 »

//Some code
while($qry2=mysql_fetch_array($qry1)){
$graph=$qry2[0];

How to make the $graph an array.

The values in $graphs should be made an array of graphValues();
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

Post by kreoton »

Code: Select all

while($qry2=mysql_fetch_array($qry1)){
$graph[]=$qry2[0];
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

kreoton wrote:

Code: Select all

while($qry2=mysql_fetch_array($qry1)){
$graph[]=$qry2[0];

How to push the values into array.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Was that last a question?

Assuming that ...

Code: Select all

$graph = array();
... on the previous line, then

Code: Select all

$graph[]=$qry2[0];
... will append a new array element with the assigned value.
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

Stryks wrote:Was that last a question?

Assuming that ...

Code: Select all

$graph = array();
... on the previous line, then

Code: Select all

$graph[]=$qry2[0];
... will append a new array element with the assigned value.

while($qry2=mysql_fetch_array($qry1)){
$graphValue[]=$qry2[0];

$qry2[0] contains around 143 records of values.

But count($graphValue) result is 1.

$graphvalue=array(contents of $qry2[0]);

How to take all the values into array
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Lets see here...

It seems to me that you must be only returning 1 row, or you haven't closed you loop and then broken out of it further down.

I also notice that when transferring the value to the array (which you are in fact doing) you aren't saving $qry2, you are saving $qry2[0]. This is saving the first returned field to the array, not entire rows of data. Is this what you expect?

Insert this code and post back the results. Maybe it'll give a clearer idea of whats going on.

Code: Select all

	$graphValue = array();
	
	while($qry2 = mysql_fetch_array($qry1)) {
		$graphValue[] = $qry2[0]; 	// saving only the first returned column (field)
	}
	
	echo "<br>Returned " . mysql_num_rows($qry1) . " rows from database. \$graphValue contains " . count($graphValue) . " rows.<br><br>";
	var_dump($graphValue);
Good luck. 8)
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

Code: Select all

But $qry2[0] contains the values.
If i use just $qry2

I am getting the errors
Unsupported operand types in /var/www/........... on line ....

The values should be stored in $graphValues=array(1,34,564,763,6754.............)

I am not able to store the values into $graphValues[/syntax]
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

What line were you getting that error on? One of the lines you have given as an example, or somewhere else?

Have you tried inserting the code I suggested? What was the output?

Inside the while loop, $qry contains the columns returned from the database that belong to a single row. By setting the value in the while loop to $qry[0] you are just setting a new array element to the value of the first returned column for that row. There is no problem with that, so long as all you are wanting to get from the database is that one value (in which case you should change your query to specify the column name instead of the *).

In order to help you, we need to get a better idea of what data is being returned by the query.

The output of the snippet I posted would be a good start.
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[syntax="sql"]select avg(bw_util) from `tablename` where `fieldname` <  now() and `fieldname` > date_sub(now(),interval 1 day) group by `fieldname`

Output is:
Array ( [0] => 0.01248649 [avg(bw_util)] => 0.01248649 )


The values of avg(bw_util) is around 143 records.

This values should be passed to the array called graphValues.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Ok, but now I have to ask how you got that output.

Can you please post the code that you have already shown and include the code that outputs what you have just provided.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

To clarify, Your query is pulling the average of a column from a group of rows. You could well have mulitple results. There could well be 143 results.

However, the output you provided seems to suggest that only one row is being returned, that I assume is the average across those 143 records.

You were getting an array count of 1 before, which leads me to think I'm correct in this assumption.

If I am correct, you will not be able to access those 143 individual records unless you go back to a query more like your original.

What my code was going to provide, among other things, was an understanding of how many rows were being returned from the database so that we could assess what the problem was. Either for some reason the way we are saying to populate an array is not working like it should, or you are in fact just returning one row.

Details will help us help you. :)
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[syntax="sql"]
SELECT avg(bw_util) FROM `tablename` WHERE `fieldname` <  now() AND `fieldname` > date_sub(now(),interval 1 day) GROUP BY `fieldname`
[/syntax]

Code: Select all

$qry1=mysql_query($qry);
print_r(mysql_fetch_array($qry1));
while($qry2=mysql_fetch_array($qry1)){
$graphValues[]=$qry2[0]
}
The output of print_r(mysql_fetch_array($qry1)); is
Array ( [0] => 0.01248649 [avg(bw_util)] => 0.01248649 )


and

Code: Select all

$qry1=mysql_query($qry);
while($qry2=mysql_fetch_array($qry1)){
$graphValues[] =$qry2[0];
print_r(mysql_fetch_array($qry1));

}
the output is
Array ( [0] => 0.03561714 [avg(bw_util)] => 0.03561714 )
Array ( [0] => 0.02123514 [avg(bw_util)] => 0.02123514 )
Array ( [0] => 0.05216757 [avg(bw_util)] => 0.05216757 )
Array ( [0] => 0.00991143 [avg(bw_util)] => 0.00991143 )
Array ( [0] => 0.01082432 [avg(bw_util)] => 0.01082432 )
Array ( [0] => 0.04801351 [avg(bw_util)] => 0.04801351 )
Array ( [0] => 0.00705405 [avg(bw_util)] => 0.00705405 )
Array ( [0] => 0.00750811 [avg(bw_util)] => 0.00750811 )


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Ok ... replace this:

Code: Select all

$qry1=mysql_query($qry);
print_r(mysql_fetch_array($qry1));
while($qry2=mysql_fetch_array($qry1)){
$graphValues[]=$qry2[0]
}
... with this :

Code: Select all

        $graphValue = array();
       
        while($qry2 = mysql_fetch_array($qry1)) {
                $graphValue[] = $qry2[0];       // saving only the first returned column (field)
        }
       
        echo "<br>Returned " . mysql_num_rows($qry1) . " rows from database. \$graphValue contains " . count($graphValue) . " rows.<br><br>";
        var_dump($graphValue);
And post back the results. (I haven't tested it, so if theres an error just let me know.)
Post Reply