Arrays
Moderator: General Moderators
Code: Select all
while($qry2=mysql_fetch_array($qry1)){
$graph[]=$qry2[0];kreoton wrote:Code: Select all
while($qry2=mysql_fetch_array($qry1)){ $graph[]=$qry2[0];
How to push the values into array.
Was that last a question?
Assuming that ...
... on the previous line, then
... will append a new array element with the assigned value.
Assuming that ...
Code: Select all
$graph = array();Code: Select all
$graph[]=$qry2[0];Stryks wrote:Was that last a question?
Assuming that ...
... on the previous line, thenCode: Select all
$graph = array();... will append a new array element with the assigned value.Code: Select all
$graph[]=$qry2[0];
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
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.
Good luck. 
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);
Code: Select all
But $qry2[0] contains the values.
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]
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.
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.
feyd | Please use
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]
[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]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.
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.
feyd | Please use [/syntax]
The output of print_r(mysql_fetch_array($qry1)); is
Array ( [0] => 0.01248649 [avg(bw_util)] => 0.01248649 )
and
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]
[syntax="sql"]
SELECT avg(bw_util) FROM `tablename` WHERE `fieldname` < now() AND `fieldname` > date_sub(now(),interval 1 day) GROUP BY `fieldname`Code: Select all
$qry1=mysql_query($qry);
print_r(mysql_fetch_array($qry1));
while($qry2=mysql_fetch_array($qry1)){
$graphValues[]=$qry2[0]
}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));
}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]Ok ... replace this:
... with this :
And post back the results. (I haven't tested it, so if theres an error just let me know.)
Code: Select all
$qry1=mysql_query($qry);
print_r(mysql_fetch_array($qry1));
while($qry2=mysql_fetch_array($qry1)){
$graphValues[]=$qry2[0]
}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);