Fetching SQL Current date for Graph lib php from timestamp - php

So i have setup the code to display graph from php sql. But i cant seem to order it by todays date from a timestamp in my table. Here is my code:
<?php
include("phpgraphlib.php");
$graph=new PHPGraphLib(550,450);
$link = mysql_connect('xxx', 'xxx', 'xxx')
or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
$dataArray=array();
//get data from database
// $sql="SELECT rating, COUNT(*) AS 'datentry' FROM main_table GROUP BY rating";
$sql="SELECT rating, COUNT(*) AS 'datentry' FROM main_table GROUP BY rating";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["rating"];
$count=$row["datentry"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Daily");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
What i want to achieve is to be able to display just todays data. This specific code displays all of the data in my table.

Related

Bar Graph using the Data from MySQL Database

Hello Everyone Good Afternoon
as of now here's what I've got
Database: Election2016
Table Name: Candidate_Info
Here is the 2 of my fields in my Table that I think is important in creating my Output
candidatename[varchar] and numberofvotes[integer]
as of now heres my data
candidatename numberofvotes
Person1 24
Person2 1
From this Scratch How can i create a Bar Graph?
Im sorry for asking this easy question. Im only a beginner in this thing.
Im using PHP and it will be used in website
any help is appreciated TY
As of now here is my code
inside mysql_graph_bar.php
<?php
include("phpgraphlib.php");
$graph=new PHPGraphLib(550,350);
$link = mysql_connect('localhost', 'root', '')
or die('Could not connect: ' . mysql_error());
mysql_select_db('election2016') or die('Could not select database');
$dataArray=array();
//get data from database
$sql="SELECT * from candidate_info group by position";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["salesgroup"];
$count=$row["count"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("candidates");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
inside bargraph.php
<html>
<h3>This is where I want to display my graph</h3>
<img src="mysql_graph_bar.php" />
</html>

Download a file in PHP

I'm trying to download a PowerPoint file. I'm saving its path in column slide in a table called lesson. Whenever I try to download it, it downloads the whole table.
All I want is the column slide, how can I do that?
// connect to the database
$link = mysql_connect('localhost','root','');
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
// query the server for the file
$L_ID = $_GET['id'];
$query = "SELECT * FROM lesson WHERE LID = '$L_ID'";
$result = mysql_query($query) or die(mysql_error());
// define results into variables
$name=mysql_result($result,0,"Lname");
$content=mysql_result($result,0,"slide");
header("Content-disposition: attachment; filename=$name");
echo $content;
mysql_close();
If all you want is the column slide, you have to select only that column in your select clause. Right now you're selecting all of the table's columns by using SELECT *.
Try this:
$query = "SELECT slide FROM lesson WHERE LID = '$L_ID'";
That should only return the column slide from table lesson.

MySQL: Count the rows per day and show the results date and count on graph?

I have a table with an INT column containing numbers, and a date column. I want to be able to count the number of same date columns are there per day. That is, how many distinct date rows there are in each day. So, for example, if an date has a record twice or thrice, it's count should be displayed on graph with date on X-Axis and count on Y-Axis
Example Data:
ID date picker
1 02-Apr-2014
2 02-APR-2014
3 04-APR-2014
4 07-APR-2014
5 07-APR-2014
6 07-Apr-2014
which graph Should i use to get this required output on graph ???
My php code i used ::
<?php
include("phpgraphlib/phpgraphlib.php");
include("phpgraphlib/phpgraphlib_stacked.php");
$graph=new PHPGraphLib(550,350);
$link = mysql_connect('localhost', 'root', '')
or die('Could not connect: ' . mysql_error());
mysql_select_db('ac_xamp_le') or die('Could not select database');
$dataArray=array();
//get data from database
$sql="select datepicker, count(id) from ac_member group by datepicker";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$date=$row["datepicker"];
$count=count('id');
//add to data areray
$dataArray[$date]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Enquires by date");
$graph->setLine(true);
$graph->setLineColor("navy");
$graph->setLineColor("255,255,0");
$graph->setLineColor("#339900");
$graph->setDataValues(true);
$graph->setGradient("blue", "navy");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>

How to select database and use JOIN with MySQL PHP database

I'm trying to run this function but can't get it to work, what am I doing wrong (output says my fetch array arg is invalid)?
$link = mysql_connect('localhost', 'over_app', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) { echo "Your Pic ID: $row['PicID']"; }
Note The Pic table is in the Pics database and the Seen table is in the SeenPics database
mysql_query returns a boolean false is the query fails .. do this
$result = mysql_query("SELECT Pic.PicID FROM Pic LEFT JOIN Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) {
echo "Your Pic ID: $row['PicID']";
}
Don't use mysql_* See this comment
Update
Now that we have the full facts - ie your attempting a join across multiple databases ... the problem is that your query isnt complete ... try this :
SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID

How to echo the result of my query? PHP & MySQL

so I have this query:
SELECT COUNT( * ) AS cnt
FROM table
WHERE datetime > NOW( ) - INTERVAL 45
SECOND
When I preform this query in MySQL I get the result: cnt 25 (http://puu.sh/7ZNh.png)
I now want to echo this in my PHP page, how would I do this?
UPDATE:
Here is the full code
<?php
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$query = ("SELECT COUNT(*) FROM usersonline WHERE datetime > NOW() - INTERVAL 5 MINUTE");
$result = mysql_query($query);
?>
list($count) = #mysql_fetch_row(mysql_query($sql));
echo $count;
Within your PHP application you need to setup a database object which can be used to connect to your database in order to execute your queries. A good place to start is with PHP PDO, more info # http://us.php.net/manual/en/pdo.query.php along with some good examples.
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('Could not connect: '.mysql_error());
}
mysql_select_db("db", $con);
$query = ("SELECT COUNT(*) FROM usersonline
WHERE datetime > DATESUB(NOW(),INTERVAL 5 MINUTE) ");
$result = mysql_query($query);
if (!$result)
{
die('Error in select statement: '.mysql_error());
}
$row = mysql_fetch_array($result);
echo "number of rows = ".htmlentities($row['cnt']);

Categories