Page 1 of 1
Arrays
Posted: Thu Sep 20, 2007 4:21 am
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();
Posted: Thu Sep 20, 2007 4:43 am
by kreoton
Code: Select all
while($qry2=mysql_fetch_array($qry1)){
$graph[]=$qry2[0];
Posted: Thu Sep 20, 2007 4:56 am
by sandy1028
kreoton wrote:Code: Select all
while($qry2=mysql_fetch_array($qry1)){
$graph[]=$qry2[0];
How to push the values into array.
Posted: Thu Sep 20, 2007 5:52 am
by Stryks
Was that last a question?
Assuming that ...
... on the previous line, then
... will append a new array element with the assigned value.
Posted: Thu Sep 20, 2007 6:01 am
by sandy1028
Stryks wrote:Was that last a question?
Assuming that ...
... on the previous line, then
... 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
Posted: Thu Sep 20, 2007 7:53 am
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.

Posted: Thu Sep 20, 2007 11:16 pm
by sandy1028
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]
Posted: Thu Sep 20, 2007 11:55 pm
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.
Posted: Fri Sep 21, 2007 12:06 am
by sandy1028
feyd | Please use 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
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]
Posted: Fri Sep 21, 2007 12:12 am
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.
Posted: Fri Sep 21, 2007 12:30 am
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.

Posted: Fri Sep 21, 2007 12:32 am
by sandy1028
feyd | Please use 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
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]
Posted: Fri Sep 21, 2007 12:41 am
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.)