Need data from mysql to draw graph - php

I want to draw a graph where value will be taken from MySQL database. it is not working. But if I give the manual value then the graph is showing. Below is my code:
<?php
include "libchart/classes/libchart.php";
header("Content-type: image/png");
$chart = new LineChart();
$con=mysqli_connect("localhost","root","","bkash");
$result = mysqli_query($con,"SELECT time,trx_value FROM dialer_rate where mno='tnr'");
$serie1 = new XYDataSet();
while($row = mysqli_fetch_array($result))
{
$serie1->addPoint(new Point($row['time']->time, $row['trx_value']->trx_value));
}
$dataSet = new XYSeriesDataSet();
$dataSet->addSerie("TNR", $serie1);
$dataSet->addSerie("ROBI", $serie2);
$dataSet->addSerie("LNK", $serie3);
$dataSet->addSerie("AIR", $serie4);
$chart->setDataSet($dataSet);
$chart->setTitle("bKash Success/Failure for All MNO");
$chart->getPlot()->setGraphCaptionRatio(0.62);
$chart->render();
?>
i am not getting any value in time & trx_value in below line...
while($row = mysqli_fetch_array($result))
{
$serie1->addPoint(new Point($row['time']->time, $row['trx_value']->trx_value));
}
but if i want to print the value using echo below output comes
while($row = mysqli_fetch_array($result))
{ echo $row['time'];
echo $row['trx_value'];
};
Then output is coming..
also graph is showing when i am using manual value like below.
$serie1->addPoint(new Point("06-01", 273));
$serie1->addPoint(new Point("06-02", 421));
$serie1->addPoint(new Point("06-03", 642));
$serie1->addPoint(new Point("06-04", 799));
$serie1->addPoint(new Point("06-05", 1009));
$serie1->addPoint(new Point("06-06", 1106));

You've got quotes around $row['time']->time that shouldn't be there, and it looks like you're trying to "double-index" your columns (i.e. get values using both ['field'] and ->field).
new Point("$row['time']->time", $row['trx_value']->trx_value)
should probably be
new Point($row['time'], $row['trx_value'])
Notice how Stack Overflow's syntax highlighter suggests the first issue. If your text editor doesn't support syntax highlighting, it might be worthwhile to switch to one that does.

Related

How to export data from MySQL to CSV?

I am trying to export data from MySQL to CSV using PHP from a website. When a user clicks generate the report it will automatically create and this will be downloaded as a file on their computer. The code below I have been following an online tutorial but I have run into some issues. Currently, it will download but will only show one row with one date value from the MySQL data.
I was wondering what I need to do to show all the data in the csv file and how do I ensure that the names of the Rows are printed as well.
#header("Content-Disposition: attachment; filename=record.csv");
$select = "SELECT * FROM DBtable WHERE user_id=$id";
$result2 = $conn->query($select);
while ($row = $result2->fetch_assoc()) {
$data = $row['pain']."\n";
$data = $row['sleep']."\n";
$data = $row['mood']."\n";
$data = $row['heartrate']."\n";
$data = $row['time_of_entry']."\n";
}
echo $data;
exit();
Instead of setting the $row values to $data, you should echo instead
#header("Content-Disposition: attachment; filename=record.csv");
$select = "SELECT * FROM DBtable WHERE user_id=$id";
$result2=$conn->query($select);
while($row=$result2->fetch_assoc()){
echo $row['pain']."\n";
echo $row['sleep']."\n";
echo $row['mood']."\n";
echo $row['heartrate']."\n";
echo $row['time_of_entry']."\n";
}
exit();
Your code just keeps setting $data to a rows value, without actually doing anything with it. Each line overwrites the previous, so when you do echo $data, you are echoing the last value it was set to which in your case was the last loops "time of entry" value.
As for adding "names of the Rows", currently the code puts in a value then a new line. You probably want to replace "\n" with ",", so one loops data is one one line, then before the closing while loop, echo a "\n" to insert a new line character. You can then put the titles in the same way, using an echo, and comma seperated titles, followed by a new line character.
For example
echo $row['pain'].",";
echo $row['sleep'].",";
echo $row['mood'].",";
echo $row['heartrate'].",";
echo $row['time_of_entry'].",";
echo "\n";

PHP ODBC return all records

I started using PHP with Oracle using ODBC commands. I used odbc_exec to get to query the database, which seems to work.
I am trying to print the dataset to a text file. I am able to write to the text file, but for some reason, I am only able to get back 1 record/row from the table.
One query in particular returns over 100 records. I would like to be able to print all 100 records in the text file.
I have researched all over the web, and I have come close to getting this work. In fact, I can print all the records to the web page. I just can't get all of the records into the text file.
Please see my code below and advise what I am missing.
<?php
$voyage = $_POST['voyage'];
$query = "SELECT * FROM voyageTable WHERE voyage = '".$voyage."'";
$result = odbc_exec($connect, $query);
while($row = odbc_fetch_array($result)) // have tried odbc_fetch_row already
{
$fullName = odbc_result($result, 1);
}
$dateFile = "CMDU-".$voyage."-".date('dmY').".txt";
$dataString = $fullName . "\n";
$fWrite = fopen($dateFile, "a");
$wrote = fwrite($fWrite, $dataString);
fclose($fWrite);
?>
I know I am close to getting this.
I have altered the while loop numerous times. I tried to do this as well:
<?php
while(odbc_fetch_array($result))
// ...
?>
Please help.
That was unsuccessful as well.
Instead of
$fullName = odbc_result($result, 1);
You need to either echo out like this:
echo odbc_result($result, 1);
Or store the result in an array like this:
$fullName[] = odbc_result($result, 1);
Just storing the value of each row in $fullname as a variable will overwrite the value and give you the last value from the last row in the result set.
Expanding on ಠ_ಠ's comment.

Error with my dojo ajax php request

Im trying to use a dojo ajax function to call a PHP file that then returns the contents of a DB table in JSON format.
My function:
var _getWeatherInfo = function(){
dojo.xhrget({
url: "PHP/weather.php?ntown=" + _ntown,
handleAs: "json",
timeout: 5000,
load: function(responce, details) {
_updateWeathertData
},
error: function(error_msg, details) {
_handleError(error_msg);
}
});
}
My PHP:
<?php include('configHome.php'); ?>
<?php
$ntown = $_GET['ntown'];
$weather = array();
$query="SELECT * FROM `weather` WHERE `town` = '$ntown'";
$result=mysql_query($query);
while($row = mysql_fetch_row($result)) {
$weather[] = $row[0];
}
echo json_encode($weather);
mysql_close();
?>
When using this code I am getting an error message saying that "$ntown = $_GET['ntown'];" is an undefined index. I have tried removing the index all together and using an actual value in the select statement (i.e. SELECT * FROM weather WHERE town = 'Auckland') but all I get back is the value i enter ["Auckland"], and not the 3 other values that are meant to be returned, ["Auckland", "Sunny", "8", "14"].
Any ideas? I can try add more info if needed. Thanks!
There are some other issues with your code, but to get to the one you are asking the question about. You have this:
while($row = mysql_fetch_row($result)) {
$weather[] = $row[0];
}
What you are doing is just taking the first value of the row (of which there is probably only one, and just sending that back. This is what you need:
$weather = mysql_fetch_row($result);

MYSQLI Returns only 1 value while there's 2

I've started making a Video script that loads the videos using MySql and I am using Mysqli.
However, There's 2 Rows that it should post, but it only post the second none, not the first one.
It loads the results using "Brand" so if there's 2 rows named "Test", it only loads the second one, but not the first one.
So, what is causing this? I've tried with 3 rows, and it did not include the first row.
Code
<?php
{ /* Global Data */
ini_set('display_errors', 0);
ini_set('error_reporting', -0);
$GetPath = $_GET['b'];
$SqlUser = "root";
$SqlPass = "**Private**";
$SqlHost = "localhost";
$SqlData = "heisteknikk";
}
{ /* Mysql Connect */
$Sql = new mysqli($SqlHost, $SqlUser, $SqlPass, $SqlData);
if ($Sql->connect_error) { die("Sorry, Could not connect (".$Sql->connect_errno.") ".$Sql->connect_error);}
}
{ /* Test */
$Brand = $Sql->real_escape_string($GetPath);
$SqlData = "SELECT * FROM videos WHERE Brand = '".$Brand."'";
$SqlQuery = $Sql->query($SqlData);
if (!$SqlQuery) {
echo $Sql->error;
}
if ($SqlQuery->num_rows == 0) {
die("Nothing was found");
}
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
echo "<table border='1'>";
while ($Heis = $SqlQuery->fetch_assoc()) {
echo "
<tr><td>".$Heis['Brand']."</td><td>".$Heis['Name']."</td><td>".$Heis['Location']."
";
}
echo "</table>";
$Sql->close();
}
?>
This line causes the bug:
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
With this line you effectively throw out the first row of the result set, as you don't process $Data at all in the code below. Just remove it, and your script should work fine (I assume that closing </td></tr> sequence was lost when pasting the code, am I right?)
Comment out (or better yet, remove) this line:
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
It is grabbing the first row...then you aren't doing anything with $Data and then grabbing the second (and consecutive rows) for display in your while loop. Commenting out that line above will make your loop grab and display them starting at the first one.
In your code
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
echo "<table border='1'>";
while ($Heis = $SqlQuery->fetch_assoc()) {
you're fetching a row (with fetch_array), throwing it away and then fetching another row (with fetch_assoc). That's probably why you're only seeing one row instead of two

displaying mysql data in marquee

i want to make an news bar, by selecting mysql data and display it in and marquee, the problem that all the data are displayed at the same time in different lines, what i need is to display the data line by line.
code:
$news = mysql_query("SELECT ann_title, ann_text FROM o_postnews_conference");
while ($row = mysql_fetch_assoc($news)) {
echo "<marquee style='float:bottom;'><font color='snow'>{$row['ann_title']}: {$row['ann_text']}</font></marquee>";
It's because you're creating a new marquee element every iteration of your while loop. Use something like the below code instead.
$news = mysql_query("SELECT ann_title, ann_text FROM o_postnews_conference");
echo "<marquee style='float:bottom;'><font color='snow'>";
while ($row = mysql_fetch_assoc($news))
echo "{$row['ann_title']}: {$row['ann_text']} ";
echo "</font></marquee>";
Although you should know that marquee has been deprecated and you should be using CSS3/javascript instead.

Categories