Graphs in PHP errors - php

I am trying to fetch from the database data and to generate a graph. I am using phpgraphlib, http://www.ebrueggeman.com/phpgraphlib/documentation/tutorial-mysql-and-phpgraphlib. I downloaded the libgd from https://github.com/libgd/libgd/releases
and also the phpgraphlib. I have PHP 5.5 and the php_gd2.dll extension I think it is enabled by default in the php.ini file.
I get 2 errors:
Warning: require_once(phpgraphlib.php): failed to open stream: No such
file or directory in C:\XAMPP\htdocs\graph.php on line 3
Fatal error: require_once(): Failed opening required 'phpgraphlib.php'
(include_path='.;C:\XAMPP\php\PEAR') in C:\XAMPP\htdocs\graph.php on
line 3
I also undertsand that something goes wrong the path and the directories. Any help? Thanks in advance!!!
<?php
require_once("phpgraphlib.php");
$graph=new PHPGraphLib(400,300);
//connection to MySQL database
$link =mysql_connect('localhost','root','') or die('Could not connect :'. mysql_error());
//select db
mysql_selected_db('mynewdb') or die('Could not select database');
$dataArray=array();
//get data from database
$sql="SELECT year1,year2 * FROM scores WHERE crit='interesting semester' AND sid='13'";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$crit=$row["crit"];
$year1=$row["year1"];
$year2=$row["year2"];
//add to data array
$dataArray[$crit]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Interesting Semester");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>

please place your file phpgraphlib.php in ./ or C:\XAMPP\php\PEAR

Related

How to connect PostgreSQL and PHP on mac?

I am new to PHP. I want to use the database stored in pgSQL from my PHP file. Please tell how to integrate pgSQL and PHP, so that I can use pgSQL in PHP using pg_connect(). My php.info() is showing MySQL, pgSQL, SQLite enabled under PDO support. But when I use pg_connect() it says:
Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: role "postgres" does not exist in /Users/username/Sites/index.php on line 3
Warning: pg_last_error(): No PostgreSQL link opened yet in /Users/username/Sites/index.php on line 4
Warning: pg_last_error(): supplied resource is not a valid PostgreSQL link resource in /Users/username/Sites/index.php on line 4
Could not connect:
I have used this tutorial to run PHP on Mac. But I couldn't find a tutorial clearly showing how to integrate PHP and pgSQL.
https://www.dyclassroom.com/howto-mac/how-to-install-apache-mysql-php-on-macos-mojave-10-14
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=localhost dbname=ipl user=postgres password=password")
or die('Could not connect: ' . pg_last_error());
// Performing SQL query
$query = 'SELECT * FROM player';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
pg_free_result($result);
// Closing connection
pg_close($dbconn);
?>
I got the problem. My username is not postgres but my name. I must have set it while installing Postgres. Therefore it is saying no role as 'postgres' exist.

Warning: mysql_connect(): Access denied for user

I am a beginner .
I copied a simple php page on the server web http://www.barman-team.ir/tt.php
and i import sql data base and make user & and pass on data base (in cpanel)
this page work on local host( xamp) but dont run on server
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body dir="rtl">
<?php
$link=mysql_connect("localhost","uname","pass");
mysql_select_db("barmante_ebram",$link);
$q="SELECT * FROM site_sug";//
$r=mysql_query($q,$link);
$line= mysql_fetch_array($r);
//while($line== $result->fetch_assoc()){
echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";
//echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
//}
?>
</body>
</html>
error log is:
Warning: mysql_connect(): Access denied for user 'barmante_ebram_u'#'localhost' (using password: YES) in /home/barmante/public_html/tt.php on line 11
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 12
Warning: mysql_query() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 14
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/barmante/public_html/tt.php on line 15
Use this kind of debugging to trace error.
I guess your all warning is related to USERNAME or PASSWARD. use correct username and password. and it will resolve problem.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
Use mysql_error() with each query so that you will get to know what is causing problem ( useful for beginner )
The first error is saying that your password to the username your supplied meaning that it can't connect to your mysql and the other error are saying that you should have provide resource to the 2nd parameter. You just interchanged the sequence of the parameters. Use the code below
<?php
$link=mysql_connect("localhost","uname","pass");
mysql_select_db($link,"barmante_ebram");
$q="SELECT * FROM site_sug";//
$r=mysql_query($link);
$line= mysql_fetch_array($r);
//while($line== $result->fetch_assoc()){
echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";
//echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
//}
?>
Note*: Don't use mysql_* functions, it is deprecated . Use mysqli or PDO prepared statements
Hope this helps you
Assuming that you have correct permissions for the user, the following code should work. It will tell you whether you have error in connecting to the sql server, or there is any error in your query.
<?php
$db_hostname = 'localhost';
$db_database = 'barmante_ebram';
$db_username = 'uname';
$db_password = 'pass';
$db_link= new MySQLi($db_hostname, $db_username, $db_password, $db_database);
if(! $db_link) //if($db_link->connect_errno > 0)
die("Could not connect to database server\n" + $db_link->connect_error);
$query = "SELECT * FROM site_sug";
$result = $db_link->query($query);
if(!result)
die("Error in query".$db_link->error);
$row = $result->fetch_assoc();
echo $row['site_suggestion_id'].$line['site_suggestion'];
?>
Note: Please always do error checking (if( !successful) die(" ")). Your code does not have any. Its a good practice to follow.

phpgraph lib not showing any output

Im using phpgraph lib to create graphs on my linux server. I tried an example and it worked, but I had provided it with the data.
then I wanted to connect it to mysql database and plot a query, when I run it, nothing happens, I don't see any output on the page or any errors, I don't see any output on the page at all, even if I put wrong credentials to my database e.t.c any inputs?
I have executed the sql statement on sql server and it's working fine.
the version of php the server has is PHP 5.3.3
<?php
include('phpgraphlib.php');
$graph= new PHPGraphLib(550,350);
$link = mysql_connect('localhost', 'user', 'password')
or die('Could not connect: ' . mysql_error());
mysql_select_db('databasename' or die('Could not select database');
$dataArray=array();
//get data from database
$sql="my sql statement";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["var1"];
$count=$row["count"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Sales by Group");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
I fixed it, I was expecting to see errors on the webpage, but didn't see any on CHROME, I hen opened it in IE and saw error 500.
Troubleshooted through the log file.
Turned out the sql statement wasn't suppose to have double quotes e.g instead of
where name="john"
it's suppose to be
where name='john'

Accessing MySQL database - D3

I have a basic line graph and attempting to access a mySQL database. I'm going wrong somewhere with the PHP and/or how handling the JSON returned - can anyone help shed some light?
At the moment the graph is not displaying at all and getting error message "Uncaught Syntax Error: Unexpected token >" for the html tag - no idea why as syntax is correct as far as I can see?
http://bl.ocks.org/5fc4cd5f41a6ddf2df23
"getdata.php" as follows:
<?php
$username="******";
$password="******";
$host="********";
$link=mysql_connect($host,$username,$password)or die("Unable to connect to MySQL");
#mysql_select_db($link) or die( "Unable to select database");
$result = mysql_query("SELECT reading, COUNT(TYPE) AS 'type' FROM TestSourceSampleData ");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[$r['reading']] = $r['type'];}
echo json_encode($rows);
mysql_close();
?>
Try placing the svg tag in the static part; I think the xmlns is missing. You could make the svg: namespace at the top.
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="290">
Also check by typing in getdata.php in your browser (to look whether the data is correctly delivered).
This seems to have done the trick - while statement was worded and formatted incorrectly:
getdata.php is now as follows:
<?php
$username="***";
$password="****";
$host="*****";
$link=mysql_connect($host,$username,$password)or die("Unable to connect to MySQL");
mysql_select_db("****", $link) or die( "Unable to select database" );
$result = mysql_query("SELECT reading FROM TestSourceSampleData", $link)
or die ("Unable to run query");
while ($row = mysql_fetch_assoc($result))
{
$reading = $row["reading"];
echo json_encode($row);
}
mysql_close($link);
?>

Accesing XAMPP MySql Database from Another Computer

So a friend of mine and I are using both xampp on ubuntu, if that helps, to connect between each other's website, We both created the same php file to connect, so we use de IP of the other, but then it says an error
Warning: mysql_connect() [function.mysql-connect]: Host 'coke-laptop.local' is not allowed to connect to this MySQL server in /opt/lampp/htdocs/connection.php on line 2
Could not connect: Host 'coke-laptop.local' is not allowed to connect to this MySQL server
We have this code on the connection.php file:
<?php
$link = mysql_connect('10.100.161.37','root','');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//echo 'Connected successfully';
$db_selected = mysql_select_db('Prueba', $link);
if (!$db_selected) {
die ('Can\'t use Prueba : ' . mysql_error());
}
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname = 'fox';
// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT * FROM Agencia");
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['ID'] . " ";
echo $row['Nombre'] . "\n\r";
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
mysql_close($link);
?>
If we use the IP just like that, we can enter each others xampp normal welcome page.
Check you have enabled remote access to the MySQL server. Open the my.cnf file (probably found inside xampp/etc/), go to the [mysqld] section and add the following (using your own ip address instead of the example)
bind-address=192.168.1.100
If there is a line that says skip-networking, comment that out so it looks like this:
# skip-networking
then restart the MySQL server
It looks like your MySQL database isn't allowing you to connect remotely with the credentials you provided. You will need to configure a remote user to connect. Try looking into MySQL Grants.
For Example:
GRANT SELECT, INSERT ON database.* TO 'someuser'#'somehost';

Categories