Accessing MySQL database - D3 - php

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);
?>

Related

my program is not getting validated in jsonlint validater

<?php$host="localhost";
$db="profile";
$user="root";
$pass="";
$connection=mysql_connect($host,$user,$pass);
if(!$connection){
die("Database server connection failed.");
}else{
$dbconnect=mysql_select_db("profile",$connection);
if(!$dbconnect){
die("Unable to connect to the specified database!");
}else{
$username=$_GET["username"];
$password=$_GET["password"];
$query="SELECT id,profile_id,username,password from home where username='$username'and password='$password'";
$resultset=mysql_query($query,
$connection);
$records=array();
if($resultset===false){
die(mysql_error());
}
while($r=mysql_fetch_row($resultset)){
$records[]=$r;
}
echo json_encode($records);
}
}?>
and error is <?php$host="localhos^Expecting '{', '['
please explain me where is the problem in my script.
I m using $_get[] method to enter detail through url in my database but I m getting error. It works in localhost but not working in validator. I just want to run it in jsonlint. What I do I have to do to correct it ?
You are indeed missing a space between <?php and $host as the parser is saying it to you in your error.
please keep a space between
<?php$host="localhost"; sholud be corrected as `<?php $host="localhost";`

Outputting contents of database mysqli

Hi I know this is a little general but its something I cant seem to work out by reading online.
Im trying to connnect to a database using php / mysqli using a wamp server and a database which is local host on php admin.
No matter what I try i keep getting the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given when i try to output the contents of the database.
the code im using is:
if (isset($_POST["submit"]))
{
$con = mysqli_connect("localhost");
if ($con == true)
{
echo "Database connection established";
}
else
{
die("Unable to connect to database");
}
$result = mysqli_query($con,"SELECT *");
while($row = mysqli_fetch_array($result))
{
echo $row['login'];
}
}
I will be good if you have a look at the standard mysqli_connect here
I will dont seem to see where you have selected any data base before attempting to dump it contents.
<?php
//set up basic connection :
$con = mysqli_connect("host","user","passw","db") or die("Error " . mysqli_error($con));
?>
Following this basic standard will also help you know where prob is.
you have to select from table . or mysqli dont know what table are you selecting from.
change this
$result = mysqli_query($con,"SELECT *");
to
$result = mysqli_query($con,"SELECT * FROM table_name ");
table_name is the name of your table
and your connection is tottally wrong.
use this
$con = mysqli_connect("hostname","username","password","database_name");
you have to learn here how to connect and use mysqli

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'

PHP postgreSql cannot display results

Please can someone help me with this I cant find very much information on this problem.
I am connecting fine to PostgreSql database but when I look through the array and display nothing is being displayed, however the exact amount of rows are being displayed s oi know the connection/query are the right syntax, must be the variables syntax but ive tried everything I can find to make it work, any ideas?
<?php
pg_connect("host=******** port=**** dbname=****** user=***** password=********") or die("Couldn't Connect"); // Connect to the Database
$query = "SELECT * FROM phones";
$query = pg_query($query);
while($row = pg_fetch_array($query))
{
echo "Model: ".$row['Model']."<br />";
echo "OS: ".$row['OS']."<br />";
echo "Description: ".$row['Description']."<br /><br />";
}
?>
Thank you in advance for any help
Try to use pg_last_error();
<?php
$dbconn = pg_connect("dbname=publisher") or die("Could not connect");
// Query that fails
$res = pg_query($dbconn, "select * from doesnotexist");
echo pg_last_error($dbconn);
?>
http://us2.php.net/pg_last_error
Try pg_fetch_assoc instead of pg_fetch_array, it might work. It works for MySQL.

mysql_fetch_array problem

i am trying to do a guestbook in php but i am having some problems with mysql_fetch_array function. I don't understand why. I try to debug by putting
die("Error ".mysql_error()) but nothing prints out. I guarantee that all my variables are correctly initialized.
Here is my code :
<?php
$nbmessagesPP = 10;
mysql_connect(HOST, USER,PASSWORD) or die( "Unable to connect to database");
mysql_select_db(DBNAME) or die ("Unable to select database!");
.......
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$first_msg = ($page - 1) * $nb_of_Page;
$query = 'Select * from livredor ORDER BY id DESC LIMIT '.$first_msg.', '.$nbmessagesPP;
$rep = mysql_query($query) or exit("Error in query".mysql_error());
$v = true;
while($v){
$v = ($data = mysql_fetch_array($rep) or die ("Error fetching the data : ".mysql_error()));
echo "<p>id -> ".$data['id']."</p>";
echo "<p>pseudo ->".$data['pseudo']."</p>";
echo "<p>messages ->".$data['message']."</p>";
echo "<hr/>";
}
mysql_close();
?>
Can someone help me ;)
Your code doesn't deal with errors or the last row correctly. When $v is false, it still goes on to print some data. It would be better rewritten as:
while (($data = mysql_fetch_array($rep))) {
echo
...
}
That forces the evaluation of the fetch before moving on to the printing.
The problem is that you're trying to access elements of the result that don't exist. mysql_fetch_array returns a regular array, with integer indices. What you want is mysql_fetch_assoc, which returns an associative array.
Edit: You also have the problem Chris describes, not dealing with the last row correctly.
Generally, if you're receiving an error saying "supplied argument is not a valid MySQL result resource" it means that your MySQL query has failed, therefor not returning a valid result resource.
Try to echo out $query before sending it through mysql_query(), then try placing the echo'd query into phpMyAdmin and see if it returns any results.
Ok i have found the problem.
The problem was that in another page i had a mysql_connection and in that page i was creating a new one.
I just catch the return value of mysql_connect function and then close it with mysql_close function at the end. Like this :
<?php
$link = mysql_connect(HOST, USER,PASSWORD) or die( "Unable to connect to database");
mysql_select_db(DBNAME) or die ("Unable to select database!");
.....
while($data = mysql_fetch_array($rep)) {//i do something here}
mysql_close($link);
?>
Thanks for your answers folks :)

Categories