简体   繁体   中英

No output from neo4j query

I am trying to execute the sort of example Neo4j code as found on here: http://neo4j.com/docs/developer-manual/current/cypher/clauses/create/#create-create-a-full-path however I seem to be missing something obvious. What I need created is (A) has (B).

$n4 = 'curl -H "Accept: application/json; charset=UTF-8" -s -u user:pass -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d \'%s\'';

function basicQuery($query){
global $n4;

    $str = '{"query" : "'.$query.'","params" : {}}';

    return sprintf($n4,$str);
}

$A = array('Label'=>'Attributes');

$B = array('Label'=>'Attributes');

$query = 'CREATE p =(A '.json_encode($A).')-[:HAS]->(B '.json_encode($B).') RETURN p';

echo shell_exec(basicQuery($query));

But I don't get any output and when I run: shell_exec(basicQuery('MATCH (A) RETURN DISTINCT count(A) AS tally'));

I get a tally of 0. I'm very new to neo4j so please can someone tell me what I'm doing wrong?

Ok the issue here appeared to be CURL. By using placeholders within my query such as A {Name:{Placeholder}} this seemed to generate output. The solution to solving problems like this appears to be copying the output into the command line and try it out there as well as in PHP.

1) You can't use quote around property key name

2) You need correctly encode a query

$n4 = 'curl -H "Accept: application/json; charset=UTF-8" -s -u user:pass -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d \'%s\'';

function basicQuery($query){
    global $n4;
    $json = array(
      "query" => $query,
      "params" => new stdClass
    );
    $str = json_encode($json);
    return sprintf($n4, $str);
}

function objToMap($obj) {
  $tmp = [];
  foreach($obj as $key=>$value) {
    $tmp[] = '`' . $key . '`: ' . json_encode($value);
  }
  return '{' . join(',', $tmp) . '}';
}

$A = array('Label'=>'Attributes');
$B = array('Label'=>'Attributes');

$query = 'CREATE p =(A:TEST ' .objToMap($A).')-[:HAS]->(B:TEST '.objToMap($B).') RETURN p';
echo shell_exec(basicQuery($query));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM