PHP code example of kynx / gqlite

1. Go to this page and download the library: Download kynx/gqlite library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kynx / gqlite example snippets


use Kynx\GqLite\Graph;
use Kynx\GqLite\ValueObject\Edge;
use Kynx\GqLite\ValueObject\Node;

// replace with path to GraphQLite extension installed above
$extensionPath = getenv('GRAPHQLITE_EXTENSION_PATH');

// Get a connection to an in-memory graph database
$graph = Graph::connect($extensionPath, ':memory:');

// Add some nodes and edges
$graph->nodes->upsert(new Node("alice", ["name" => "Alice", "age" => 30], "Person"));
$graph->nodes->upsert(new Node("bob", ["name" => "Bob", "age" => 25], "Person"));
$graph->edges->upsert(new Edge("alice", "bob", "KNOWS", ["since" => 2020]));

// Query with Cypher
$results = $graph->query('MATCH (a:Person)-[:KNOWS]->(b) RETURN a.name AS a, b.name AS b');
foreach ($results as $row) {
    echo $row['a'] . ' knows ' . $row['b'] . "\n";
}

// outputs:
// Alice knows Bob

$results = $graph->query('MATCH (a:Person)-[:KNOWS]->(b) RETURN a, b');
foreach ($results as $row) {
    $a = Node::fromArray($row['a']);
    $b = Node::fromArray($row['b']);
    
    echo $a->id . ' knows ' . $b->id . "\n";
}

// Outputs:
// alice knows bob

$results = $graph->query(
    'MATCH (a {name: $name})-[:KNOWS]->(b) RETURN a, b',
    ['name' => $_GET['name']]
);