当使用两个where子句进行Select时,为空响应



我的XMLRPC功能是这样工作的,检查我的网站上是否已经存在内容,如果它是最新的,并在必要时更新它。为了做到这一点,我检查它的数字(两种内容类型都可以有相同的数字),它的哈希(唯一)和类型。

之前我只有一种内容,我的函数工作正常,但现在我需要检查内容类型,而不仅仅是数字和哈希值。这就是问题所在,当我试着这么做的时候,我得到了一个空的答案。我的函数现在是这样的:

$query = db_query("SELECT (table_num.field_num) FROM (table_num, table_type) WHERE table_num.entity = table_type.entity AND table_num.field_num = :num AND table_type.field_type = :type", array(':num' => $num, ':type' =>$type));
 foreach ($query as $record) {
if($record->rowCount()==0) return 0;
else {
$query = db_query("SELECT (table_hash.field_hash) FROM (table_hash, table_type) WHERE table_hash.entity = table_type.entity AND table_hash.field_hash = :hash AND table_type.field_type = :type", array(':hash' => $hash, ':type' =>$type));
 foreach ($query as $record) {
if($record->rowCount()==1) return 1;
else{
$query = db_query("SELECT (table_num.entity_id) FROM (table_num, table_type) WHERE table_num.entity = table_type.entity AND table_num.field_num = :num AND table_type.field_type = :type", array(':num' => $num, ':type' =>$type));
 foreach ($query as $record) {
 echo $record->field_entity_id; 
}
 return $record;

注:当它返回0时,它不存在于我的数据库中,1表示它是最新的,当我需要更新它时,它是实体。

很抱歉长SQL,但我刚开始使用它,我没有设法让它更短。有人知道为什么我得到一个空的答案吗?

您构建查询的方式是危险的,并且会导致SQL注入,因为您没有转义您的变量。

代替:

$query = db_query("SELECT (table_num.field_num) FROM table_num WHERE table_num.field_num = $num AND table_type.field_type = $type");

你可以使用:

$query = db_query("SELECT (table_num.field_num) FROM table_num WHERE table_num.field_num = :num AND table_type.field_type = :type", array(':num' => $num, ':type' =>$type));

这样Drupal就会在插入变量之前对它们进行消毒,这样你就知道它不会导致任何漏洞。

我真的不完全明白你的问题在问什么——如果你能澄清一点,我看看我是否也能帮上忙。

最新更新