Welcome Guest, Not a member yet? Register   Sign In
Looping through DB Fields
#1

[eluser]Richard Testani[/eluser]
I'm building an SQL query with the or_where and coming up with some oddities, maybe someone can help me out with.

The following function accepts an array for each field to search through with another value set elsewhere as the string.
Code:
public function set_fields($fields) {
        $f    =    "";
        $i = 0;
        foreach($fields as $val) {
            if($i == 0) {
                $f .= "`$val` LIKE '%$this->text_value%' ";
            } else {
                $f .= " OR `$val` LIKE '%$this->text_value%' ";
            }
            $i++;
        }
        $this->db->where($f);
        
    }
Code:
SELECT `first`, `last`, `email`, `address`
FROM (`contacts`) WHERE `first` LIKE '%search contacts%'
OR ` last` LIKE '%search contacts%'
OR ` email` LIKE '%search contacts%'
OR `is_volunteer` = '1'
OR `is_donor` = '0'
OR `is_sponsor` = '0' LIMIT 100, 100

The error returned is:
Code:
Unknown column ' last' in 'where clause'
Notice the space before the column name which it seems to add.
On another occasion using a loop and where(name, 'value') would drop the '=' on all but the first loop.

Any ideas on this?
Thanks
Rich


Messages In This Thread
Looping through DB Fields - by El Forum - 08-12-2009, 04:03 PM
Looping through DB Fields - by El Forum - 08-12-2009, 04:19 PM
Looping through DB Fields - by El Forum - 08-12-2009, 04:25 PM
Looping through DB Fields - by El Forum - 08-12-2009, 05:32 PM
Looping through DB Fields - by El Forum - 08-12-2009, 07:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB