有人请帮助我,为什么我总会得到这个错误,解决方法是什么:
失败:SQLSTATE [42000]:语法错误或访问冲突:1064您 您的SQL语法有错误;检查对应的手册 您的MySQL服务器版本,以便在“5”附近使用正确的语法 第1行
我写这个类有点自动化我的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));
}
}
这就是我使用它的方式:
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');
效果很好。
知道可能出现什么问题吗?
答案 0 :(得分: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,语法错误就会很明显。