我正在使用PDO和&amp ;;基于所选择的表格参数生成查询。准备好的陈述我检测出哪些值存在,然后生成适当的PDO子语句,然后通过在其间插入'AND'来压缩子语句数组,将它们连接在一起。查询在单独和某些组合中工作正常,但许多组合只获得查询的一部分结果 - 我已经确认一切都可以通过制作语句和绑定参数,但不知何故它只是忽略了部分查询。以下是一些例子以及我要回来的内容:
SELECT * FROM songtable WHERE (ENERGY BETWEEN :energy_from AND :energy_to) AND (GENRE = :genre OR GENRE2 = :genre OR GENRE3 = :genre) ORDER BY ARTIST ASC LIMIT 0, 7
返回的数据:正确执行流派搜索,完全忽略了能量。删除类型,能量搜索工作,反之亦然。
SELECT * FROM songtable WHERE (ENERGY BETWEEN :energy_from AND :energy_to) AND BANGER = :banger ORDER BY ARTIST ASC LIMIT 0, 7
返回的数据:正确地发现'bangers'完全忽略能量,移除它们并且它们正常工作。
SELECT * FROM songtable WHERE BANGER = :banger AND (GENRE = :genre OR GENRE2 = :genre OR GENRE3 = :genre) ORDER BY ARTIST ASC LIMIT 0, 7
返回的数据:查找正确的流派并忽略了banger。再次,删除它们并且它们正常工作。
所有查询在phpmyadmin中运行正常 - 问题似乎只发生在多个条件组合在一起时 - 但是某些分组会返回所需的结果....我是否遗漏了有关PDO如何工作的内容?我不应该使用PDO吗?
完全相关来源:
function query($link, $query, $params) {
echo $query;
$stmt = $link->prepare($query);
if(count($params->getValues()) > 0) {
foreach($params->getValues() as $key => $value) {
$stmt->bindParam($key, $value);
}
}
$stmt->execute();
return $stmt;
}
function getFilter($selection, $params, $sorting, $page) {
$link = createLink('songs');
$selection = "ID, ARTIST, GENRE, GENRE2, GENRE3, TITLE, WARMUP, PEAK, BANGER, FLOORFILLER, SONGKEY, ENERGY, BPM, TRACKURL, DATETIME, FILENAME";
if($link != null) {
$query = makeQuery("SELECT", $selection, $params, $sorting, $page);
$result = query($link, $query, $params);
}
return $result->fetchAll();
}
function makeQuery($operation, $selection, $params, $sorting, $page) {
return $operation . " " . $selection . " FROM songtable " . $params . $sorting . $page;
}
class FormParams {
private $query;
private $values;
private $sorting;
private $page;
private $user;
//takes all existing arguments and fills
public function __construct($data = array()) {
if($this->valid('energy_from', $data) && $this->valid('energy_to', $data)) {
$this->values[':energy_from'] = $data['energy_from'];
$this->query[] = "(ENERGY BETWEEN :energy_from AND " . $data['energy_to'] . ")";
} else if($this->valid('energy_from', $data)) {
$this->values[':energy_from'] = $data['energy_from'];
$this->query[] = "(ENERGY BETWEEN :energy_from AND 10)";
} else if($this->valid('energy_to', $data)) {
$this->query[] = "(ENERGY BETWEEN 1 AND " . $data['energy_to'] . ")";
}
if($this->valid('bpm_from', $data) && $this->valid('bpm_to', $data)) {
$this->values[':bpm_from'] = $data['bpm_from'];
$this->values[':bpm_to'] = $data['bpm_to'];
$this->query['bpm_from'] = "(BPM BETWEEN :bpm_from AND " . $data['bpm_to'] . ")";
} else if($this->valid('bpm_from', $data)) {
$this->values[':bpm_from'] = $data['bpm_from'];
$this->query['bpm_from'] = "(BPM BETWEEN :bpm_from AND 200)";
} else if($this->valid('bpm_to', $data)) {
$this->values[':bpm_to'] = $data['bpm_to'];
$this->query[] = "(BPM BETWEEN 60 AND " . $data['bpm_to'] . ")";
}
if($this->valid('key_from', $data)) {
$this->values[':key_from'] = $data['key_from'];
$this->query['key_from'] = "KEY > :key_from";
}
if($this->valid('key_to', $data)) {
$this->values[':key_to'] = $data['key_to'];
$this->query[] = "KEY < :key_to";
}
if($data['set_placement'] == 1) {
$this->values[':warmup'] = "1";
$this->query[] = "WARMUP = :warmup";
}
else if($data['set_placement'] == 2) {
$this->values[':peak'] = "1";
$this->query[] = "PEAK = :peak";
}
if(array_key_exists('banger', $data)) {
$this->values[':banger'] = "1";
$this->query[] = "BANGER = :banger";
}
if(array_key_exists('floorfiller', $data)) {
$this->values[':floorfiller'] = "1";
$this->query[] = "FLOORFILLER = :floorfiller";
}
if($this->valid('genre', $data)) {
if($data['genre'] == '1') {
$this->values[':genre'] = 'Bass';
} else if($data['genre'] == '2') {
$this->values[':genre'] = 'House';
} else if($data['genre'] == '3') {
$this->values[':genre'] = 'Juke';
} else if($data['genre'] == '4') {
$this->values[':genre'] = 'B more';
} else if($data['genre'] == '5') {
$this->values[':genre'] = 'Trap';
} else if($data['genre'] == '6') {
$this->values[':genre'] = 'Twerk';
}
$this->query[] = "(GENRE = :genre OR GENRE2 = :genre OR GENRE3 = :genre)";
}
if($this->valid('artist', $data)) {
$this->values[':artist'] = $data['artist'];
$this->query[] = "ARTIST = :artist";
}
if($this->valid('title', $data)) {
$this->values[':title'] = $data['title'];
$this->query[] = "TITLE = :title";
}
if($this->valid('column', $data) && $this->valid('order', $data)) {
$this->sorting = new Sorting($data['column'], $data['order']);
} else {
//default sort - change to whatever youd like to initially load.
$this->sorting = new Sorting("ARTIST", "ASC");
}
if($this->valid('page', $data)) {
$this->page = new Page($data['page'], "7", null);
} else {
//default page - assume first page with default settings
$this->page = new Page("1", "7", null);
}
if($this->valid('user', $data)) {
$this->user = $data['user'];
}
//implement filtername?
unset($data);
}
/*public function getSorting(){
return $this->sorting;
} */
public function getPage(){
return $this->page;
}
public function getUser(){
return $this->user;
}
public function getSort(){
return $this->sorting;
}
public function setPageTotal($total) {
$this->page->setTotal($total);
}
public function valid($key, $array) {
if(array_key_exists($key, $array)) {
if($array[$key] != "" && $array[$key] != "0") {
return true;
}
} else {
return false;
}
}
public function getValues() {
return $this->values;
}
public function __toString() {
return (count($this->query) > 0) ? "WHERE " . implode("\r\n AND ", $this->query) : "";
}
}