JPGraph mysql_fetch_array

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
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

JPGraph mysql_fetch_array

Post by JakeJ »

I don't have a specific question, this is just for the search engine since I had difficulty locating an answer to my question and I want to post something specific for anyone else.

If you're using JPGraph and can't quite figure out why my_sql_fetch_array() doesn't work in place of array(), here's your solution.

The short version is that you have to read the data returned by my_sql_fetch_array() in to a new array().

Code: Select all

<?php
$sql = mysql_query(SELECT * from table);
While ($row = mysql_fetch_array($sql)) {
    global $data, $label; //if you don't declare these globals, the new arrays will not be usable outside of this loop
    $data[] = $row['balance'];
    $label[] = $row['creditor_name'];
}
$graph = new PieGraph(400,400);
$p1->SetLegends($label);
?>
Note that the above code is only that which related to populating the arrays for the graph and the legend. Adapt as necessary.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: JPGraph mysql_fetch_array

Post by JakeJ »

**this reply is just to get it off of the unanswered list** 8)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: JPGraph mysql_fetch_array

Post by John Cartwright »

Unless that code is contained within a function or class where $data and $label are not defined, then it should work fine. In fact, it is bad practice to use the global keyword.
Post Reply