How do I use Google Charts with mySQL data? - php

I'm working on a website that stores information in databases using mysql. I want to display that information using interactive diagrams via google charts. The problem is that I cant figure out how to use the mysql data with google charts. I did some research and found that I need to encode the data in JSON format but I'm not sure as to how I can do that. Any help would be greatly appreciated.
Thanks.

(one method - there are probably many)
Use php to create the JSON strings from your query and pass these to google charts on the client.
(pcode)
<?php
$rows = PDO->query( "select col0, col1 from table" );
echo "jsonvar = {";
foreach( $rows as $row )
{
echo "{" . $row->col0 . "," . $row->col1 . "},";
}
echo "};";
?>
// some call to google charts on the client
gchart.make_a_cool_graph( jsonvar, otherparams,... );

Related

Updating Google Sheets through PHP

I'm stuck on a project. I want to update a column in Google Sheets using PHP but I'm unable to figure it out.
Here is my code:
<?php
$url = 'https://sheets.googleapis.com/v4/spreadsheets/1MWmhO8PyjwxgeELR-NZg4Y9WhhGwaf6Wt-hD7gtadcg/values/AppList!$A:$H?key=AIzaSyDdkIF9YlaAvgIqv66sA2Hci4LZSrmGjLM';
$json = json_decode(file_get_contents($url));
$rows = $json->values;
foreach($rows as $row) {
var_dump($row);
}
?>
With this code, I'm able to fetch the data from Google Sheets to PHP but I don't know how to update from PHP to Google Sheets column Status.

Can I get the full result of Google's speech to text transcription as JSON from the php library?

I'm using google's php api (https://github.com/googleapis/google-cloud-php) for speech to text transcription and am getting everything to work so far. However; all the examples on using the php library show the results being handled like this:
if ($op->operationSucceeded()) {
$response = $op->getResult();
// each result is for a consecutive portion of the audio. iterate
// through them to get the transcripts for the entire audio file.
foreach ($response->getResults() as $result) {
$alternatives = $result->getAlternatives();
$mostLikely = $alternatives[0];
$transcript = $mostLikely->getTranscript();
$confidence = $mostLikely->getConfidence();
printf('Transcript: %s' . PHP_EOL, $transcript);
printf('Confidence: %s' . PHP_EOL, $confidence);
}
}
I would really like the full result as json so I can easily store it in a database table. Is there a way to get the full result returned as json?
Thanks!
You can call serializeToJsonString() on any object inheriting from Google\Protobuf\Internal\Message. Make sure you're using a relatively recent release of google/cloud.
Additionally, if you're only using Cloud Speech, google/cloud-speech might be better, as it'll install a much smaller package.

PHP API for android using mysql

So, currently i am using firebase for storing my app data online,
I would like to create my own database,
so i was planning to get a 100gb bandwidth hosting plan with php and mysql (is that bandwidth enought) per download, my app downloads approximately 0.4MB of data (as per firebase).
So, to create the api, i just have to encode the mysql data into json and print it ? then my android app will read it and use it ? is this the best method ?
$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
or is there any other, more efficient method to do this ?
Yes but you should send response code like 200,403 as well.
here is a similar question
How to write a REST API?

insert into phpMyAdmin - loop array

sorry, im a beginner. Still learning the basics of php.
Here is where i'm stuck. I'm using php to get the results and sending the results onto the database(phpMyAdmin).
I've set up a loop to get the results
i'm trying to get the above results to be sent to phpMyAdmin. i cannot work out on how to do it. I guess i need INSERT INTO somewhere inside the loop so that it can send each into phpMyAdmin There are about 10 different questions. they are displayed on the php but do not know where to go ahead from here
thanks for your help :)
edit
Thanks for the guide, I cannot seem to work out where mysql_query will go inside the loop.
mysql_query("INSERT INTO.......
Assuming u have connected to DB and has a proper table in the DB*.I am just modifying your code* so make sure about the value of $_POST['something'] and the way it should behave.
$con=mysqli_connect("localhost","username","password","dbname") or
die("Error " . mysqli_error($con));
if (isset($_POST['something']))
{
foreach ($_POST['something'] as $key => $value)
{
echo 'something #' . $key . ' You gave it ' . $value . '<br>';
$a="insert into tablename(columnname) values('$value')";
$b=mysqli_query($con,$a);
}
}
else
{
echo 'no options selected';
}
To connect to a database, you need to use the PHP mysqli_* functions. You can find more informations on the PHP online manual.
You can also use PDO (PHP Data Objects). Here's the link to a very good tutorial : http://www.phpeveryday.com/articles/PHP-Data-Object/PDO-Tutorial-P842.html
Both ways are valid and secure.

Display database contents? PHP / MySQL

So I have a chatroom type of database where the text that a user inserts gets stored into a databse as their username in one field and their message in the other. I want to have my page output the database info, so that people can see each others messages.
How do I do this?
Also, is it possible to make a for loop that checks to see if the database has been updated with a new message, therefore it reloads the page? (Then the page outputs the database info again to update everyones messages)
Please help.. i'm so confused.
Take a look at MySQL functions in PHP manual. You need to connect to the server/database and run a select query to get the data from tables.
As for the loop: you could use JavaScript setInterval function and combine that with AJAX call to periodically poll for new records.
Like the others have said, you will want to connect to your database and then query the table that you have the data in.
while($row = mysql_fetch_assoc($results))
{
echo $row['username'] . " said: " . $row['message'] . "<br />";
}
I use mysql_fetch_assoc() instead of mysql_fetch_array() since the arrays are associative arrays (not indexed by integers, but rather by names (associations))
As for displaying the update on the page dynamically, that involves AJAX. Basically what that means is that your page will call out to a background script to get the new records from the database. This would require a new field in your 'messages' table, something like 'msg_delivered' that you could set to '1' when it has been fetched.
You should check out this if you are interested in making an AJAX chat client: http://htmltimes.com/javascript-chat-client-in-jquery.php
To read anything from a mysql database you would use the mysql_connect() and the mysql_query() functions
eg:
$link = mysql_connect('localhost', 'root', '');
$results = mysql_query('select * from messages');
while($row = mysql_fetch_array($results))
{
echo $row['username'] . ': ' . $row['message'].'<br />';
}
To display new messages the best way would be to use AJAX and poll the database from there, either loading a separate page into a DIV or getting XML back and placing into HTML tags. I would recommend using JQuery for these kinds of tasks. Check http://www.sitepoint.com/article/ajax-jquery/ for an example.

Categories