有没有办法构建类似于DOMDocument构建HTML文档的SQL查询?

时间:2016-11-26 22:54:04

标签: php mysql sql domdocument

有没有办法为HTML页面构建类似于DOMDocument的SQL查询?

语法错误的可能性太多了:

$query = "SELECT " . $fields . " FROM " . $table;
if ($condition) 
  $query .= "WHERE status > 1";

是否有第三方或股票方式来构建类似于DOMDocument的查询?

1 个答案:

答案 0 :(得分:0)

当使用普通的SQL查询时,我通常不会使用多行连接,因为它们会导致语法错误,如上所述。为一个部分添加多行时,我在新行的开头放置连接符(字段列表的逗号,来自和/或其中的连接)。

$query = "SELECT ".$fields;
$query .= " FROM ".$table;
if ($condition) {
  $query .= "WHERE status > 1";
}

对于某些使用带有join()的数组的部分派上用场:

$conditions = array();
$conditions[] = "status > 1";
// Later on
if ($conditions) {
  // Depending on your intention, you can use AND or OR here:
  $query .= "WHERE ".join(' AND ', $conditions);
}

但是有很多库以面向对象的方式构建查询。最后测试的是doctrine的查询构建器。