如何从orient db中“选择”数据并使用php显示它

时间:2017-06-03 15:36:47

标签: php database orientdb

我正在尝试从orient db数据库中选择插入的数据并将其回显出去? 我怎么能用php做到这一点?

这是我到目前为止所做的事情

require('OrientDB/OrientDB.php');
$db = new OrientDB('localhost', 2424);





        $connected = $db->connect('root', 'hello');
        $config = $db->DBOpen('tabe', 'root', 'hello');
        if($connected)
        {

              $records = $db->query("select from Nation");

              echo $records;


        }
        else
        {
           die('Server Error');
        }

它只是在屏幕上打印'Array'

我在Windows机器上运行Apache php服务器。 如何从数据库获取数据并在网站上打印。

当我用于每个循环打印元素时

我的脚本只是超时

 $records = $db->query("select from Nation");
              foreach ($records as $v)
                  echo $v;

如果我使用print_r($ records);

它提供以下输出

Array ( [0] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 169 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 169:0 [version] => 4 [content:OrientDBRecord:private] => Nation@in_partOf:%AQAAAAIAwQAAAAAAAAAAAMIAAAAAAAAAAA==;,Country_Name:"India",Iso_code:"IN" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) [1] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 170 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 170:0 [version] => 4 [content:OrientDBRecord:private] => Nation@Country_Name:"United States of America",Iso_code:"US" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) [2] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 171 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 171:0 [version] => 2 [content:OrientDBRecord:private] => Nation@Country_Name:"Canada",Iso_code:"CA" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) )

如何将其转换为合适的形式。比如只取名字 enter image description here

这是我的表

2 个答案:

答案 0 :(得分:1)

更好地使用 PhpOrient 库它是由orient db官方支持的库

    $client = new PhpOrient();
        $client->hostname = 'localhost';
        $client->port     = 2424;
        $client->username = 'your_username';
        $client->password = 'your_password';

        $client->connect();
        $client->dbOpen('Your_db_name');

         $query = "SELECT FROM NATION";
         $result = $client->query($query);


       $result =  json_encode($result);
       $result = json_decode($result);

foreach ($result as $row){
       echo $row->oData->Country_Name;
       echo $row->oData->Iso_code;
}

phporient:https://github.com/orientechnologies/PhpOrient.git

答案 1 :(得分:0)

打印结果。而不是echo

print_r($records);

要迭代结果,请使用foreachfor

首先使用foreach作为:

检查我们是否有一些结果
if(count($records) > 0){//check if we have a result
   foreach($records as $value){//loop
     print_r($value);
  }
}