Tutorials - Database - Page 1
This tutorial will guide you thorugh making a chart using data from a database.
I am using a MySQL database, but this code can be modified to use a different database.
My first request is that you follow this tutorial, that you make the temporary table and copy some data into it (don't worry,
we'll delete the table later.) I get a lot of emails asking how to do this and most people get stuck trying
to do too many steps at once. They have all the code, but the unfamiliarity of it gets them into a pickle!
To unpickle ourselves we'll break the problem down into little steps. This follows the way I code in real life (I code a lot
at work and for this project) and I hope you will get to the last tutorial and find you have a chart that works and
a lot less respect for my coding skills (haha!)
So, connect to your database and make a new table called 'ofc_sourceforge_downloads'.
I use sqlyog which is a GUI front end for MySQL, I
connect and right click on my database and 'add new table'. Easy.
Add three columns, id (int auto increment), date (date) and downloads (int). Here is the SQL to do that:
Code:
CREATE TABLE `sourceforge_downloads`
( `id` int NOT NULL AUTO_INCREMENT , `date` date , `downloads` int , PRIMARY KEY (`id`));
This table may now be in with all your other tables (clients, customers and stuff) don't worry about that.
MySQL is just a cool file system. Think of tables as nice files, it is cheap to add or delete and play around with them.
Don't worry too much about it and don't treat it any differently than your desktop.
Next populate the table with some data:
Code:
INSERT INTO `ofc_sourceforge_downloads`(`id`,`date`,`downloads`)
VALUES
(1,'2009-02-01',7381),
(2,'2009-01-01',7921),
(3,'2008-12-01',9499),
(4,'2008-11-01',14958),
(5,'2008-10-01',10600),
(6,'2008-09-01',9407),
(7,'2008-08-01',4694),
(8,'2008-07-01',4717),
(9,'2008-06-01',6267),
(10,'2008-05-01',6496),
(11,'2008-04-01',6211),
(12,'2008-03-01',5804),
(13,'2008-02-01',5287),
(14,'2008-01-01',5042),
(15,'2007-12-01',4375),
(16,'2007-11-01',3346),
(17,'2007-10-01',1933),
(18,'2007-09-01',1378),
(19,'2007-08-01',1106);
Now for some code. I'm using PHP here.
gallery/tutorial-db.php
<?php
include 'includes/db.php';
/* My connection details are hidden in the above file. You should uncomment the following and add your user name and password: $con = mysql_connect('localhost', 'user', 'password'); $db = mysql_select_db('my_database_name', $con); */ $result = mysql_query("select * from ofc_sourceforge_downloads order by date");
$data = array(); $max = 0; while($row = mysql_fetch_array($result)) { $data[] = $row['downloads']; }
print '<pre>'; print_r( $data ); print '</pre>';
Save this file (call "ofc-chart.php") in the root of your webserver, you can test it
by browsing to the URL of the page ( http://example.com/ofc-chart.php )
I include this .php file into this page to run it. Here is my include:
Code:
<?php include_once 'gallery/tutorial-db.php'; ?>
and here are the results:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/teethgrinduk/public_html/open-flash-chart-2/gallery/tutorial-db.php on line 17
Array
(
)
Yay! Finally, some code and some data. Bootstrapping a database with data is boooooring.
Tutorial 2
|
Adverts:
|