使用nodejs检索时,Cassandra远程数据库不使用PHP检索数据

时间:2015-01-30 10:25:14

标签: php database node.js cassandra

我遇到了使用PHP从cassandra数据库访问数据的问题,而我可以使用nodejs访问。

这是我的PHP代码

$s = "SELECT * FROM user_project_0001 WHERE usermail='sohini.neogi#ATwitslog.com'";
        $stmt = $this->dbf->prepare( $s );
        if( !$stmt ) {
            echo "<br/>\nPDO::errorInfo():<br/>\n";
            print_r($this->dbf->errorInfo());
            echo '<br/>';
        }
        $stmt->execute();
        echo $stmt->rowCount();
        /** Plusieurs projets pour cet utilisateur **/
        if( $stmt->rowCount() > 1 ) {
            echo '<select name="project_name" id="project_name_select">';
            echo '<option value="">' . __( 'Please select', 'weenat-plugin' ) . '</option>';
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
                echo '<option value="' . $row['project_name'] . '" ';
                if( isset( $_POST['project_name'] ) && $row['project_name'] == $_POST['project_name'] )
                    echo 'selected="selected"';
                echo '>' . $row['project_name'] . '</option>';
            }
}

我收到0条记录,而如果我尝试使用nodejs访问相同的查询,我将获得3条记录。

这是nodejs代码

client.execute("SELECT * FROM user_project_0001 WHERE usermail='sohini.neogi#ATwitslog.com'", function (err, result) {
           if (!err){

               if ( result.rows.length > 0 ) {
                   console.log("Total Records = %d", result.rows.length);
                   var user = result.rows[0];
                   console.log("name = %s", user.usermail);
                   console.log("pname = %s", user.project_name);
               } else {
                   console.log("No results");
               }
           }


       });

对于PHP我使用PDO连接

$dsn = 'cassandra:host=xx.xxx.xx.xx;port=9160';
$this->dbf = new PDO($dsn);
 $this->dbf->exec("USE senskey01");

对于nodejs,这是我与cassandra驱动程序的连接

var client = new cassandra.Client({contactPoints: ['xx.xxx.xx.xx'], keyspace: 'senskey01'});

我无法理解为什么PHP无法访问数据库。

1 个答案:

答案 0 :(得分:0)

我不是一个PHP人员,但通过查看PDO的实例化以及查询,我认为它缺少密钥空间。尝试将代码修改为:

  1. 连接到特定密钥空间
  2. 或完全限定查询中表格的名称:

    $s = "SELECT * FROM senskey01.user_project_0001 WHERE usermail='sohini.neogi#ATwitslog.com'";