简体   繁体   English

通过php查询Cassandra

[英]Querying Cassandra through php

I am trying to do a SELECT query from Cassandra Database through a PHP script. 我正在尝试通过PHP脚本从Cassandra数据库执行SELECT查询。
The connection goes through php successfully. 连接成功通过php。 This is the query: 这是查询:

$session  = $cluster->connect($keyspace);    
$result = $session->execute("SELECT * FROM twitter.tweets_by_author WHERE author= 'John Doe'")

Question 1 问题1
I will get the author name through an HTML form , let the variable be author_name . 我将通过HTML表单获取作者姓名,让变量为author_name How do I pass author_name in the WHERE clause? 如何在WHERE子句中传递author_name

I have tried: 我努力了:

$result = $session->execute("SELECT * FROM twitter.tweets_by_author WHERE author= \%s\",author_name);

but this isn't working, most probably due to the fact that the value after = in WHERE clause needs to be in single inverted commas '' for it to be valid cassandra query. 但这是行不通的,这很可能是由于WHERE子句中=之后的值必须以单个反斜杠''开头,以使其成为有效的cassandra查询。

Further, I want to print the result, something like this: 此外,我想打印结果,如下所示:

printf("  \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \n  ", $row['tid'],$row['tweet_text'], $row['author_id'], $row['location'], $row['lang']);

Question 2 问题2
Since HTML will combine extra whitespaces how do I print whitespaces according to my wish? 由于HTML将合并额外的空格,我如何根据自己的意愿打印空格?

Answering Q1 only (I'm not HTML expert). 仅回答Q1(我不是HTML专家)。 You better to use so-called prepared queries as it's described in documentation . 您最好使用文档中介绍的所谓的预备查询。 Something like this: 像这样:

$prepared = $session->prepare(""SELECT * FROM twitter.tweets_by_author WHERE author= ?")
$result = $session->execute($prepared, 
      array('arguments' => array('author' => author_name)))

One of the advantages of the prepared queries is that they are parsed once on server, and then client send only the data for placeholders ( ? marks), not the full query. 准备好的查询的优点之一是,它们在服务器上解析一次,然后客户端仅发送占位符( ?标记)的数据,而不发送完整查询。 This gives very big performance boost. 这极大地提高了性能。 But you need to store prepared query somewhere, so it will be reused by multiple requests - it requires an additional roundtrip to server... 但是您需要将准备好的查询存储在某个地方,因此它将被多个请求重用-它需要服务器之间的额外往返。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Curl / PHP查询API - Querying API through Curl/PHP 通过PHP SQL Joomla 3中的多个表进行查询 - Querying through multiple tables in PHP SQL Joomla 3 通过Mongo php查询Meteor.users() - Querying Meteor.users() through Mongo php 通过PHP查询数据库(有些麻烦) - Querying the database through PHP (some trouble) 通过PHP查询从mysql表打印值的最佳方法 - Best way to print values from mysql table by querying through PHP 在PHP中通过Hive / Thrift查询数据库不起作用 - Querying database through Hive/Thrift in PHP does not work 未定义的属性和PHP致命错误:通过unixODBC / FreeTDS从php查询mssql时,允许的内存耗尽 - Undefined Property and PHP Fatal Error: Allowed memory exhausted when querying mssql from php through unixODBC/FreeTDS PHP+MS 访问:通过 PHP 查询时,通配符 * 被视为字符串? - PHP+MS Access: Wildcard * being taken as a string when querying through PHP? 如何检查表中是否已有名称? 查询Mysql每天的总和,然后通过PHP填充HTML数据 - How to check if name already in the table? Querying Mysql for sum of each day and then populating an HTML data through PHP with it 通过Apache服务器上的php / ajax查询大型xml文件时的性能问题 - Performance issue when querying a large xml file through php/ajax on Apache Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM