0

For some reason the both code upon killing the query var returns:

SELECT client_fname FROM client WHERE c=:l AND f=:n

Instead of:

SELECT client_fname FROM client WHERE client_id=:id AND client_id=:fname

Notice that only the first character of the column name strings is output.

Where am I going wrong? :S

PHP 5.4, will be using PDO SQL.

public function getField($_field, $_id, $_type) {
    $_args = array(
        array($_type.'_id', 'id'),
        array($_type.'_fname', 'fname')
    );
    //var_dump($_args);
    echo $this->dbSelect($_type.'_'.$_field, $_type, $_args);
}

protected function dbSelect($_select, $_from, $_args) {
    $i = 0; //var_dump($_args);
    $query = 'SELECT '.$_select.' FROM '.$_from.' WHERE ';
    foreach ($_args as $_where) {
        if($i == 0) {
            $query .= $_where[$i][0] .'=:'. $_where[$i][1];
        } else {
            $query .= ' AND '.$_where[$i][0] .'=:'. $_where[$i][1];
        }
        $i++;
    }
    die($query);
 }
4

1 回答 1

1

$_args was a 2D array. However, your foreach is using $_where as its iteration variable. $_where is itself a one-dimensional array, so you don't need $i here at all. Instead just use $_where[0]

$_where[0] should refer to the column, and $_where[1] refers to its bound placeholder. $i is unrelated to the contents of $_where.

foreach ($_args as $_where) {
    if($i == 0) {
        $query .= $_where[0] .'=:'. $_where[1];
    } else {
        $query .= ' AND '.$_where[0] .'=:'. $_where[1];
    }
    $i++;
}

Otherwise, you are getting the result of an array key access of a string. For example:

$str = "Hello world";
echo $str[0];
// "H"
于 2013-03-07T16:29:30.490 回答