<?php require_once( dirname(dirname(dirname( __FILE__ ))) . '/wp-load.php' ); ?>
<!-- START of header -->
<?php get_header(); ?>
<!-- END of header -->

<!-- warapper -->

<div class="docs-content">
    <!-- START of page navigation -->
    <?php get_template_part( 'docs_navigation' ); ?>
    <!-- END of page navigation -->
    <!-- START of pusher -->
    <div class="docs-content-body">
        <div id="content" class="docs-content-body__inner">


            <div id="outline-container-1" class="outline-3">
            <h1 id="sec-1"><span class="section-number-3">2.4.5</span> PHP Quickstart </h1>
            <div class="outline-text-3" id="text-1">

            <h3>Introduction</h3>

<p>GridDB has released a <em>database connector </em>for the PHP programming language. Like the Python, Ruby, and Go API's for GridDB, it was built using the <a title="SWIG website" href="http://www.swig.org/">SWIG</a> library. The connector supports versions of PHP 7 or higher, and can work on CentOS version 7.4 or higher.</p>

<p>PHP or <span class="ILfuVd NA6bn">Hypertext Preprocessor</span> is an open-source programming language developed by Rasmus Lerdorf.</p>

<p>In this post, we will go through common steps involved in setting up and testing a database connector for GridDB, in this case it will be for our <strong>PHP Client</strong>.</p>


<div id="toc-block" style="background: #f9f9f9 none repeat scroll 0 0; border: 1px solid #aaa; padding: 20px; width: auto; display: table; margin-bottom: 1em;">
<h5 id="toc">Table of Contents</h5>
<div class="toc_container">
<ul style="list-style-type: none !important;">
 	<li style="list-style-type: none;">
<ul style="list-style-type: none !important;">
 	<li>1    <a href="#setup-php">PHP Client Setup and Installation</a></li>
 	<li>2    <a href="#connection-php">Connecting to GridDB with the PHP Client</a></li>
 	<li>3    <a href="#row-schema">Creating Row Schemas in PHP</a></li>
 	<li>4    <a href="#query-data">Queries and Data Management</a></li>
 	<li>5    <a href="#refs">References</a></li>
</ul>
</li>
</ul>
</div>
</div>
<h3 id="setup-php">PHP Client Setup and Installation</h3>

<p>Begin by cloning the github repository for the PHP Client.</p>

<pre class="prettyprint">$ git clone https://github.com/griddb/php_client.git
</pre>

<p>To build the PHP Client, you will need to have the <a title="C client Github Page" href="https://github.com/griddb/c_client">GridDB C Client</a> built and installed. You can follow this <a href="https://griddb.net/en/blog/using-griddbs-cpythonruby-apis/#c-intro">blog post</a> on how to set up and test the C Client if you are not familiar.</p>
<ul id="PHP-version-note">
 	<li><b>NOTE: </b>before you <code>make</code> and build the PHP client on your system, ensure that you have PHP <b>version 7 or higher</b>. You can follow instructions on this <a title="Installing PHP 7 on Centos 7" href="https://linuxize.com/post/install-php-7-on-centos-7/">page</a> which explains how to get and configure PHP version 7.</li>
</ul>

<p>Now that you have obtained the source code from Github, you can simply follow the instructions in the <code>README</code> on the PHP Client’s <a title="PHP client Github repository" href="https://github.com/griddb/php_client">Github page</a>.</p>

<h3 id="connection-php">Connecting to GridDB with the PHP Client</h3>

<p>If you wish to connect to GridDB in a PHP program, simply import the PHP API with: <code> include('griddb_php_client.php');</code> The name of the GridDB package for PHP is <code>StoreFactory</code>.</p>

<p>From there you can obtain an instance of it using <code>get_default();</code> and connect it to your GridDB cluster with the factory instance's <code>-&gt;get_store</code> method. The parameters you give this method are a <code>array(</code>object. The elements in the array are simply the configuration fields you want to specify to connect to GridDB.</p>

<p>Note that the port <b>must be specified as an integer</b> while all the other database configuration values like host or cluster name can be strings.</p>

<pre class="prettyprint">&lt;?php
  include('griddb_php_client.php');
  $factory = StoreFactory::get_default();
  $update = true;
  try {
      //Get GridStore object
      $gridstore = $factory-&gt;get_store(array("notificationAddress" =&gt; $argv[1],
                        "notificationPort" =&gt; $argv[2],
                        "clusterName" =&gt; $argv[3],
                        "user" =&gt; $argv[4],
                        "password" =&gt; $argv[5]
                    ));
    } catch(GSException $e){
        echo($e-&gt;what()."\n");
        echo($e-&gt;get_code()."\n");
    }
?&gt;
</pre>

<h3 id="row-schema">Creating Row Schemas in PHP</h3>

<p>Schemas for containers and rows can be created using arrays with GridDB API types.</p>

<p>You can simply use <i>two-dimensional array</i> of interfaces to map the column types for your container. From there you can create a container schema (<code>array(array())</code>) by using <code>-&gt;put_container()</code> method. You can use and insert these <i>schemas</i> to create containers.</p>

<p>For example, if in a container the second column stores a boolean with the value of <code>false</code>, then the value should be mapped as <code>$row-&gt;set_field_by_bool(1, false);</code>.</p>

<pre class="prettyprint">// Creating container info
#Create ContainerInfo
        $conInfo =
// Creating schema for our TimeSeries Container
// The first column is always the row key
#Create TimeSeries
        $ts = $gridstore-&gt;put_container("point01", array(array("timestamp" =&gt; GS_TYPE_TIMESTAMP),
                        array("active" =&gt; GS_TYPE_BOOL),
                        array("voltage" =&gt; GS_TYPE_DOUBLE)),
                        GS_CONTAINER_TIME_SERIES);
        #Create and set row data
        $row = $ts-&gt;create_row();
        $row-&gt;set_field_by_timestamp(0, TimestampUtils::current());
        $row-&gt;set_field_by_bool(1, false);
        $row-&gt;set_field_by_double(2, 100);
        #Put row to timeseries with current timestamp
        $ts-&gt;put_row($row);
</pre>

<p>You can also obtain a <b>Row</b> with a simple call to the <code>get_next();</code> method on the container with the name of the row object. As mentioned before, a row is created using GridDB  API methods, all one needs to do to access the column value is to reference the <i>row-index</i> of the column.</p>

<pre class="prettyprint">        //Create Collection
        $col = $gridstore-&gt;put_container("col01", array(array("name" =&gt; GS_TYPE_STRING),
                  array("status" =&gt; GS_TYPE_BOOL),
                  array("count" =&gt; GS_TYPE_LONG),
                  array("lob" =&gt; GS_TYPE_BLOB)),
                  GS_CONTAINER_COLLECTION);
        //Change auto commit mode to false
        $col-&gt;set_auto_commit(false);
        //Set an index on the Row-key Column
        $col-&gt;create_index("name", GS_INDEX_FLAG_DEFAULT);
        //Set an index on the Column
        $col-&gt;create_index("count", GS_INDEX_FLAG_DEFAULT);
        //Create and set row data
        $row = $col-&gt;create_row(); //Create row for refer
        $row-&gt;set_field_by_string(0, "name01");
        $row-&gt;set_field_by_bool(1, False);
        $row-&gt;set_field_by_long(2, 1);
        $row-&gt;set_field_by_blob(3, "ABCDEFGHIJ");
        //Put row: RowKey is "name01"
        $col-&gt;put_row($row);
        $col-&gt;commit();
        $row2 = $col-&gt;create_row(); //Create row for refer
        $col-&gt;get_row_by_string("name01", True, $row2); //Get row with RowKey "name01"
        $col-&gt;delete_row_by_string("name01"); //Remove row with RowKey "name01"
        //Put row: RowKey is "name02"
        $col-&gt;put_row_by_string("name02", $row);
        $col-&gt;commit();
        //Create normal query
        $query = $col-&gt;query("select * where name = 'name02'");
        //Execute query
        $rrow = $col-&gt;create_row();
        $rs = $query-&gt;fetch($update);
        while ($rs-&gt;has_next()){
            $rs-&gt;get_next($rrow);
            $name = $rrow-&gt;get_field_as_string(0);
            $status = $rrow-&gt;get_field_as_bool(1);
            $count = $rrow-&gt;get_field_as_long(2) + 1;
            $lob = $rrow-&gt;get_field_as_blob(3);
            echo("Person: name=$name status=");
            echo $status ? 'true' : 'false';
            echo(" count=$count lob=$lob\n");
            //Update row
            $rrow-&gt;set_field_by_long(2, $count);
            $rs-&gt;update_current($rrow);
        }
        //End transaction
        $col-&gt;commit();
</pre>

<h3 id="query-data">Queries and Data Management</h3>

<p>Once you have your containers populated with data and inserted into GridDB, you are ready to query and fetch your data. Similar to the Python or Java APIs, all you need to is to construct a <b>Query object</b> (by using the query method from a container object <code>$query = $ts-&gt;query("select * ");</code>) with the specific query you would like to issue to your container. Once that is done, simply fetch the query’s results and store it in the query object.</p>

<ul>
 	<li><b>Note: </b> if you want to select specific rows to update, you must set the <i>commit-mode</i> of the container you are trying update to <i>false</i>.</li>
</ul>

<p>Once you have finished with updating row values, you can simply <code>-&gt;commit();</code> the changes.</p>

<h3>Conclusion</h3>

<p>It should now be quite easy to connect and manage your GridDB cluster using PHP. With all the various utilities offered by the GridDB PHP Client, storing data and developing high-performance PHP applications with GridDB should now be possible.</p>

<h3 id="refs">References</h3>
<ul>
<li>The source code for the GridDB PHP client can be found on the <a href="https://github.com/griddb/php_client" title="PHP client Github Page">official Github repository</a></li>
</ul>
        </div>
    </div>
</div>
<!-- / main -->

<?php get_footer(); ?>
