在Codeigniter中使用UNION并收到错误?

时间:2011-10-18 06:19:05

标签: php mysql codeigniter union

我希望选择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>

3 个答案:

答案 0 :(得分:2)

你在字符串中抛出单引号而不转义它们。那可能是你的问题。

答案 1 :(得分:1)

tour_foreign_residencetour_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 . '%"');