How to create a node in Neo4j with PHP

How to create a node in Neo4j with PHP

  • @Tsartsaris

    Posted on 2014-01-28

    ‚ÄčThere are actually 2 ways to create a node in Neo4j with PHP. I will demonstrate both of them. If you haven't setup Neo4j with PHP I suggest to follow this quick quide (Neo4j and PHP getting started). Ok lets start.The first way is by using the makeNode() function and we use it like this:

     $myfirstnode = $client->makeNode();
    Remember that if we do not call the save(); function nothing is going to be saved in our database.
    Let's take now the id of the node created with:
     $myfirstnodeID = $myfirstnode->getId();
    echo $myfirstnodeID;
    This should echo out the ID of the node created.

    The second way we can use to create a node is with cypher query language (Cypher) by using CREATE clause. In php it will be like this

     $queryString = "CREATE (n)";
    $query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
    $result = $query->getResultSet();

    With this code we will send a cypher query to the the database and simply it will create a node. nothing is going to be commited to the database until the last line until the last line

    $result = $query->getResultSet();

    is executed. To retrieve the node id we just created we simply add this line below the last line.

     foreach ($result as $row) {       
            $nodeid = $row['x']->getId();
            //echo $nodeid = $row['x']->getId();
    Perhaps some of you will think that the best way is te first because we write less code but later on you will see the POWER of CYPHER query language.

    Let's see what we have done so far. Open your preferred  internet browser and go to You should see something like this

    Neo4j webadmin node created
    Next to the $ sign on top type the following

     START n=Node(1) RETURN n

    Replace the number 1 with whatever your php script echoed before. You should see something like this

    Neo4j webadmin node preview
    Congratulations , you just created your first node.



Social Me!

Twitter Logo LinkedIn Logo Google+ Logo Tumblr Logo