CodeIgniter Forums
Database query with where clause for column string is in error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Database query with where clause for column string is in error (/showthread.php?tid=42225)



Database query with where clause for column string is in error - El Forum - 05-31-2011

[eluser]Unknown[/eluser]
I'm trying to code a query statement to DB2 database on Windows.

My code is

$sql = "select userid, name, surname from adminuser where userid = ? and password = ?";
$query = $this->db->query($sql, array($userid, $password));

==============================================================

I've got an error like this:

A Database Error Occurred
Error Number:

select userid, name, surname from adminuser where userid = 'user1' and password = 'passw0rd'

Filename: C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Code\system\database\DB_driver.php

Line Number: 330

==============================================================

If I copy the SQL statement to query directly with DB2, it works.

I tried querying another table using where clause with integer column, it works fine.

Please help,
Thank you.


Database query with where clause for column string is in error - El Forum - 06-02-2011

[eluser]Unknown[/eluser]
I found the cause of the problem.
The _execute function in db2c_driver.php call _prep_query function which call @db2_escape_string($sql).
The @db2_escape_string function will modify ' to '' that caused the error.

Commenting out the _prep_query function solved the problem.

function _execute($sql)
{ /* comment the following line out */
// $sql = $this->_prep_query($sql);
if (!$this->conn_id) echo "**** CONNECTION NOT ACTIVE ****";
$sql = str_replace("\n", ' ', $sql);
log_message('debug', "SQL: $sql");
return @db2_exec($this->conn_id, $sql);
}