0

有人请帮助我,为什么我总是得到这个错误,以及解决方案是什么:

失败:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“5”附近使用正确的语法

类.database.inc.php

我写了这个类来自动化我的 sql 查询。效果很好,除非我想将值绑定到 SQL LIMIT 操作:

class Database extends PDO
{
    private $engine;
    private $host;
    private $database;
    private $user;
    private $pass;

    private $tablename;
    private $valueArray;
    private $optionArray;

    public function __construct($engine,$charset,$host,$database,$user,$pass)
    {
        $this->engine   = $engine;
        $this->charset  = $charset;
        $this->host     = $host;
        $this->database = $database;
        $this->user     = $user;
        $this->pass     = $pass;
        $dns = $this->engine.':dbname='.$this->database.';charset='.$this->charset.';host='.$this->host;
        parent::__construct($dns,$this->user,$this->pass);
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    private function runSQL($sql=NULL,$valueArray=NULL)
    {
        $sth = $this->prepare($sql);
        if(isset($valueArray))
        {
            foreach($valueArray as $values)
            {
                foreach($values as $key => $value)
                {
                    $paramDataType = $this->getPdoParamDataType($value);
                    $sth->bindValue($key,$value,$paramDataType);
                }
                $sth->execute($values);
            }
        }
        else
        {
            $sth->execute();
        }
        return $sth;
    }

    private function getPdoParamDataType($variable)
    {
        $variableType = gettype($variable);
        switch ($variableType)
        {
            case 'integer':
                return PDO::PARAM_INT;
                break;
            case 'string':
                return PDO::PARAM_STR;
                break;
            case 'double':
                return PDO::PARAM_STR;
                break;
            case 'boolean':
                return PDO::PARAM_BOOL;
                break;
            case 'NULL':
                return PDO::PARAM_NULL;
                break;
        }
    }

    private function fetchRows($sth=NULL)
    {
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    private function getTableStructure($tableName=NULL)
    {
        $this->tableName  = $tableName;
        $sql = 'DESCRIBE '.$this->tableName;
        $tableStructureArray = $this->fetchRows($this->runSQL($sql));
            return $tableStructureArray;
    }

    public function getRecords($tableName=NULL,$options=NULL,$valueArray=NULL)
    {
        $this->tableName  = $tableName;
        $this->valueArray = $valueArray;
        $options=' '.$options;
        $columnFields = $this->getTableStructure($this->tableName);
        $sql = 'SELECT '.implode(', ',$columnFields).' FROM '.$tableName.$options;
        return $this->fetchRows($this->runSQL($sql,$this->valueArray));
    }
}

测试.php

这就是我使用它的方式:

function __autoload($class_name)  
{  
    include_once 'classes/class.' . $class_name . '.inc.php';  
} 
$pdo = new Database('mysql','utf8','localhost','database','password','');
try
{
    $valueArray = array(
                        array(':limit'=>5)
                        );
    $pdo->beginTransaction();
    $result = $pdo->getRecords('my table name','LIMIT :limit',$valueArray);
    $pdo->commit();
}
catch (Exception $e)
{
  $pdo->rollBack();
  echo "Failed: " . $e->getMessage();
}

如果我改变这一行:

$result = $pdo->getRecords('my table name','LIMIT :limit',$valueArray);

对此:

$result = $pdo->getRecords('my table name','LIMIT 5');

它运作良好。

知道有什么问题吗?

4

1 回答 1

1

这里的整个 foreach 都是 NOOP,因为在执行时传递变量会覆盖它。

foreach($values as $key => $value) {
    $paramDataType = $this->getPdoParamDataType($value);
    $sth->bindValue($key,$value,$paramDataType);
}
$sth->execute($values); // this makes the previous loop a NOOP.

我建议你删除它,也不要尝试显式设置数据类型。PDO 将推断出正确的类型。

另外,我敢打赌,如果你在准备之前回显 $sql,语法错误会很明显。

于 2013-03-12T23:22:48.373 回答