Monday, July 3, 2017

How to Use PHP and MySQL Document Store

PHP Developers can now try the MySQL Document Store by using the MySQL X DevAPI for PHP PECL Extension. Developers in other languages have had access for a while but now PHP coders can get in on the action and use the MySQL Document Store.

What Does the Code Look Like?

// Connection parameters
  $user = 'root';
  $passwd = 'S3cret#';
  $host = 'localhost';
  $port = '33060';
  $connection_uri = 'mysqlx://'.$user.':'.$passwd.'@'.$host.':'.$port;
  echo $connection_uri . "\n";

// Connect as a Node Session
  $nodeSession = mysql_xdevapi\getNodeSession($connection_uri);
// "USE world_x"
  $schema = $nodeSession->getSchema("world_x");
// Specify collection to use
  $collection = $schema->getCollection("countryinfo");
// SELECT * FROM world_x WHERE _id = "USA"
  $result = $collection->find('_id = "USA"')->execute();
// Fetch/Display data
  $data = $result->fetchAll();
Well, PHP code is PHP code. The big changes is that the developer no longer needs to use Structured Query Language to talk with the database. Now one connects to the schema of choice, sets the collection to use used, and then finds the record(s) of choice. Zero SQL involved.

PECL Extension

PECL is the PHP Community Library which houses all sorts of treasures. Now it also houses the MySQL XDevAPI extension. I have had the best luck with downloading the software and building it on my system following the directions in the README file. This is not an easy build but keep plugging and you will get it built.


dstokes@davelaptop:~/phpxdev$ php 001.php
array(1) {
  array(7) {
    string(3) "USA"
    string(13) "United States"
    array(3) {
      string(13) "North America"
      string(13) "North America"
    array(2) {
      string(14) "George W. Bush"
      string(16) "Federal Republic"
    array(2) {

Next Time

More PHP and MySQL Document Store code!


  1. These all are notable facts… I stick with responsive website design for my business. With the mobile marketing is at its peak stage, having website that goes comfy on all devices will help your business. You can check my website for more details on the importance of responsive website design. PHP Training Institute in Chennai | Best PHP training in Chennai | PHP Training Institute in Chennai

  2. These all are outstanding certainties… I stay with responsive web composition for my business. With the versatile showcasing is at its pinnacle arrange, having a website that goes comfortable on all gadgets will help your business. You can check my site for more subtle elements on the significance of responsive web composition. PHP Training Institute in Ahmedabad , PHP classes in Ahmedabad

  3. This “php ustad” tutorial blog is designed for beginners – to learn php at home step by step using simple programming examples

  4. Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Blog.Please feel free to visit our site. Thank you for sharing.
    php training center in indore
    Keep Posting:)

  5. Informative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post...
    Android application developers Chennai | Android application developers India