简体   繁体   English

使用绑定参数的PHP SQL语法错误

[英]PHP SQL Syntax Error using Bind parameters

First, I use a MySQL POO API, there is the important part : 首先,我使用MySQL POO API,其中有重要部分:

public function query($query,$params = null,$fetchmode = PDO::FETCH_ASSOC)
{
    $query = trim($query);

    $this->Init($query,$params);

    if (stripos($query, 'select') === 0){
        return $this->sQuery->fetchAll($fetchmode);
    }
    elseif (stripos($query, 'insert') === 0 ||  stripos($query, 'update') === 0 || stripos($query, 'delete') === 0) {
        return $this->sQuery->rowCount();   
    }   
    else {
        return NULL;
    }
}

_ _

This API has a loging and showing Class to save and show the SQL error, there is my error :
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? WHERE firstname = ? AND age = ? ?' at line 1
Raw SQL : SELECT * FROM users WHERE id = :iduser

After this I found the SQL included, there is the function and the code who are using this function : 在此之后,我发现其中包含了SQL,其中包含使用该功能的函数和代码:

function get_member_informations($id)
{
  global $bdd;

  $dn = $bdd->query("SELECT * FROM users WHERE id = :iduser", array("iduser" => $id));
  $dn[0]['avatar'] = base64_decode($dn[0]['avatar']);
  return $dn;
}

And the code who use this function : 和使用此功能的代码:

$profil = $bdd->query("SELECT * FROM users WHERE username = :username", array("username"=>$username));
$id = $profil[0]['id'];
$profile = get_member_informations($id);

I know it's a mysql and php issue, so to check if it was not the $id the cause of the error, i made a print_r on it but all was working as well. 我知道这是mysql和php的问题,因此要检查是否不是$ id导致错误,我在上面做了一个print_r,但一切都正常。 I don't know how to fix this, any help would be appreciated 我不知道如何解决此问题,我们将不胜感激

I'm the second dev working on it. 我是第二位开发人员。 After checking, the class was reporting the bad Raw SQL, i've fixed that and after checking, it's a : 在检查之后,该类报告了错误的Raw SQL,我已经修复了该问题,在检查之后,它是一个:

FROM :table
"table"=> $this->table

Correcting it, thank to all of you, sorry for incoveniance. 对其进行更正,谢谢大家,对于给您带来的不便深表歉意。 @dleiftah ==> Thanks for the great answers ;) @dleiftah ==>感谢您的出色回答;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM