[英]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.