Jennifer Anthony
Jennifer Anthony

Reputation: 2277

Use UNION in Codeigniter and get error?

I want select data with UNION as follow:

$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

But I encounter this error:

> 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>

How can I fix it?

Update:

Error:

> 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>

Upvotes: 0

Views: 1812

Answers (3)

Konrad Kosowski
Konrad Kosowski

Reputation: 11

Table tour_foreign_residence and tour_foreign have a different number of columns. Remove * after each select to set the same number of columns.

Upvotes: 1

xsist10
xsist10

Reputation: 3051

Change this:

$data = $this->db->query('SELECT * FROM tour_foreign_residence WHERE name LIKE '%$find%' UNION SELECT * FROM tour_foreign WHERE name LIKE '%$find%'');

to

$data = $this->db->query('SELECT * FROM tour_foreign_residence WHERE name LIKE "%' . $find . '%" UNION SELECT * FROM tour_foreign WHERE name LIKE "%' . $find . '%"');

Upvotes: 0

Jeff Day
Jeff Day

Reputation: 3717

You're throwing single quotes in the string without escaping them. That could be your problem.

Upvotes: 2

Related Questions