准备好的声明不适用于PHP

时间:2011-11-10 13:23:02

标签: php mysql prepared-statement

我正在尝试使用mysqli执行预准备语句,但该语句从不执行结果,也不会引发错误。但执行查询通常可以正常工作。

准备好的查询如下所示:

SELECT * FROM games WHERE YEARweek(game_date)=?

常规的非准备查询是

SELECT * FROM games WHERE YEARweek(game_date)= YEARweek(current_DATE) +1

任何想法为什么?

执行查询的代码位于不同的地方,但在短版本中看起来像这样:

$WHERE_CLAUSE='';
        $first=true;
        if(isset($conditions['conditions'])) {
            foreach($conditions['conditions'] as $key=>$condition){
                    if(is_array($condition)){

                    } else {
                        if($first)
                            $WHERE_CLAUSE.=$key.'=?';
                        else 
                            $WHERE_CLAUSE.=' AND '.$key.'=?';

                        $input_data[$key]=$condition;
                        $first=false;
                    }
            }//end foreach

            if(!empty($WHERE_CLAUSE)){
                $query.='WHERE '.$WHERE_CLAUSE.' ';
            }
        }

        $result=PVDatabase::preparedSelect($query, $input_data);

public static function preparedQuery($query, $data, $formats = '') {

    if (self::_hasAdapter(get_class(), __FUNCTION__))
        return self::_callAdapter(get_class(), __FUNCTION__, $query, $data, $formats);

    if (self::$dbtype == self::$mySQLConnection) {
        self::$link -> prepare($query);
        $count = 1;

        foreach ($data as $key => $value) {
            self::$link -> bindParam($count, $value);
            $count++;
        }//end foreach

        return self::$link -> execute();
    } else if (self::$dbtype == self::$postgreSQLConnection) {
        $result = pg_prepare(self::$link, '', $query);
        $result = pg_execute(self::$link, '', $data);
        return $result;
    } else if (self::$dbtype == self::$oracleConnection) {

    } else if (self::$dbtype == self::$msSQLConnection) {
        $stmt = sqlsrv_prepare(self::$link, $query, $data);
        return sqlsrv_execute($stmt);
    }

}//end preparedQuery

2 个答案:

答案 0 :(得分:4)

由于您没有提供用于调用查询的代码,我猜测您可能绑定了包含表达式的值。它不是被评估,而是按字面解释。

答案 1 :(得分:0)

PDO必须逃避第二个查询的YEARweek(current_DATE)+1部分。

请改为:

$next_year = date('Y) + 1;

SELECT * FROM games WHERE YEARweek(game_date) = $next_year