Drini
Drini

Reputation: 77

Querying Fusion-Table from PHP

My case is as follows: I have a MySQL database which I exported and inserted in Fusion Table. Now I need to modify the php page in querying the data from the Fusion Table instead of the localhost db.

I've read about querying Fusion-Tables from the Developers guide, but it refers to a GData Java Library and the SQL API. On another site I saw there were other libraries that can be used for querying, including Zend framework where PHP relies on, which is relevant in my case. However, I haven't stumbled upon any sample or tutorial which simply shows a php page on how you can query a simple cell, row or column from where I can just modify and improve the coding to apply it on the design.

My questions are:

  1. Do I need to install/upload on the hosting space some library?

  2. Is there a php sample & tutorial on querying Fusion Table data, I might have missed?

  3. Can anyone provide me with a clue how you would query for example the Latitude info of the row with ID 5? (pages are retrieved using the path: articles.php?lang=en&pg=comm_en&m=view&commID=88

Below is the coding I have in comm_en.php page, which queries from PHP to SQL the data.

Thank you in advance!

Drini

<?php
session_start();
foreach ($_REQUEST as  $key => $value){
    $$key=addslashes(htmlspecialchars(strip_tags($value)));
}
if(isset($_GET["m"]))       {$m=$_GET["m"];}            else     {$m="";}
if(isset($_GET["commID"]))      {$commID=$_GET["commID"];}    else {$commID=1;}
if(isset($_GET["lang"]))        {$lang=$_GET["lang"];}   
else {$lang="en";}

Shfaq($m,$commID);

function Shfaq($metod,$commID)
 {
switch($metod)
{
 case "view":
        {View($commID);}        
    break;
 case "default":
        {View($artID);}      
    break;

}
}  // end of Shfaq();


function View($commID)
{
     $link =mysql_connect("localhost", "db-user", "db-pass") or die ("E pamundur lidhja!");
mysql_select_db("DataBase-name") or die (mysql_error());
$queryComm = "SELECT * FROM communities WHERE id ='$commID' LIMIT 0,1";
$commRes=mysql_query($queryComm) or die(mysql_error());
$comm=mysql_fetch_array($commRes);  
$healthPerc = round(($comm["healthBooklet"]/$comm["totalChildNo"]), 3)*100 ;

echo '      <table class="gpsbox" align="right">
        <caption>GPS Location</caption>                   
        <tr><td>Latitude </td><td>'.$comm["latitude"].'</td></tr>
        <tr><td>Longitude</td><td>'.$comm["longitude"].'</td></tr> 
    </table>....<html coding continues>

Upvotes: 1

Views: 2418

Answers (2)

dan-klasson
dan-klasson

Reputation: 14190

Here is how to use the newest PHP API client library with Fusion Tables:

use Google_Service_Fusiontables;

$client_email = '<id>@developer.gserviceaccount.com';
$private_key = file_get_contents('<private key>.p12');
$scopes= array(
        'https://www.googleapis.com/auth/fusiontables',
        'https://www.googleapis.com/auth/fusiontables.readonly',
        );
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}

$service = new Google_Service_Fusiontables($client);
$result = $service->query->sql('SELECT <column> FROM <table id>');
var_dump($result);

Upvotes: 3

Odi
Odi

Reputation: 6916

  1. There is a PHP client library, you can use it if you want (it's probably the easiest way to access Fusion Tables from PHP)

  2. Next to the client library there is also PHP sample code, just take a look, it probably helps you to understand the concept.

  3. When you use the library, you can simply write sql statements, i.e. something like

    select latitude from 123456 where id = 5;
    

123456 refers to the table id of your fusion table. If your table is public, you don't even have to care about authentication, but if you want to access private tables or update/insert data, I'd recommend to use OAuth for authentication.

Sample code in general can be found on the Google Fusion Tables API Sample Code

Upvotes: 0

Related Questions