我希望选择UNION
的数据如下:
$find = 'hello';
$data = $this->db->query('SELECT * FROM tour_foreign_residence WHERE name LIKE '%$find%' UNION SELECT * FROM tour_foreign WHERE name LIKE '%$find%''); // Line number 46
但是我遇到了这个错误:
> A PHP Error was encountered<br> Severity: Warning<br> Message:
> Division by zero<br> Filename: foreign.php<br> Line Number: 46<br>
>
> A Database Error Occurred<br> The query you submitted is not
> valid.<br> Filename:
> D:\xampp\htdocs\system\database\DB_driver.php<br> Line
> Number: 257<br>
我该如何解决?
更新
错误:
> A Database Error Occurred<br> Error Number: 1222<br> The used SELECT
> statements have a different number of columns<br> SELECT * FROM
> tour_foreign_residence WHERE name LIKE "%hello%" UNION SELECT * FROM
> tour_foreign WHERE name LIKE "%hello%"<br> Filename:
> D:\xampp\htdocs\system\database\DB_driver.php<br> Line Number: 330<br>
答案 0 :(得分:2)
你在字符串中抛出单引号而不转义它们。那可能是你的问题。
答案 1 :(得分:1)
表tour_foreign_residence
和tour_foreign
具有不同的列数。每次选择后删除*
以设置相同的列数。
答案 2 :(得分:0)
改变这个:
$data = $this->db->query('SELECT * FROM tour_foreign_residence WHERE name LIKE '%$find%' UNION SELECT * FROM tour_foreign WHERE name LIKE '%$find%'');
到
$data = $this->db->query('SELECT * FROM tour_foreign_residence WHERE name LIKE "%' . $find . '%" UNION SELECT * FROM tour_foreign WHERE name LIKE "%' . $find . '%"');