简体   繁体   English

使用symfony中的参数执行sql请求时出现语法错误?

[英]Syntax error while executing a sql request with parameters in symfony?

I have a just simple problem I guess, I followed Symfony 4.3 documentation, and tried to do do exactly what they have done to execute a SQL request, but I get an error when passing parameters in the execute method, but I get no error while executing the same code but without passing parameters to the request. 我有一个简单的问题,我想,我遵循Symfony 4.3文档,并试图完成他们为执行SQL请求所做的事情,但是在execute方法中传递参数时遇到错误,但我没有得到任何错误执行相同的代码但不将参数传递给请求。

I tried this and it worked: 我试过这个并且它有效:

    $conn = $this->getDoctrine()->getManager()->getConnection();

    $sql = '
        SELECT * FROM question_comment WHERE question=2 LIMIT 0, 3';
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    var_dump($stmt->fetchAll());

But this does not work, I get a syntax error: 但这不起作用,我得到一个语法错误:

    $question = $request->query->get('question');
    $row = $request->query->get('row');

    $question = intval($question);
    $beggining = intval($row*3);

    $conn = $this->getDoctrine()->getManager()->getConnection();

    $sql = '
        SELECT * FROM question_comment WHERE question=:question LIMIT :beggining , 3';
    $stmt = $conn->prepare($sql);
    $stmt->execute(['question' => $question, 'beggining' => $beggining]);
    var_dump($stmt->fetchAll());

This is the error: 这是错误:

An exception occurred while executing ' SELECT * FROM question_comment WHERE question= :question LIMIT :beggining , 3' with params [2, 0]: 执行'SELECT * FROM question_comment WHERE question =:question LIMIT:beggining,3'with params [2,0]时发生异常:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0' , 3' at line 1 检查与MySQL服务器版本对应的手册,以便在第1行的“0”,3'附近使用正确的语法

Here is what I have followed, I did exactly what was there, but its not working. 就是我所遵循的,我确实做了那里,但它不起作用。

I tried this solution to bind my parameters but it did not work too : 我试过这个解决方案绑定我的参数,但它也不起作用:

 $sql = '
            SELECT * FROM question_comment WHERE question=:question LIMIT :beggining , 3';
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':question', $question, \PDO::PARAM_INT); 
        $stmt->bindParam(':beginning', $beginning, \PDO::PARAM_INT); 
        $stmt->execute();

And I got this : 我得到了这个:

An exception occurred while executing ' SELECT * FROM question_comment WHERE question=:question LIMIT :beggining , 3' with params [2, null]: 执行'SELECT * FROM question_comment WHERE question =:question LIMIT:beggining,3'with params [2,null]时发生异常:

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined SQLSTATE [HY093]:参数号无效:未定义参数

alternative way to bind parameters is using this. 绑定参数的替代方法是使用它。

 $stmt->bindParam(':question', $question, PDO::PARAM_STR); 
 $stmt->bindParam(':beginning', $beginning, PDO::PARAM_INT); 

or to make it sure you binded the $beginning parameter, cast it to int. 或者确保绑定了$ beginning参数,将其强制转换为int。

$stmt->bindValue(':beginning', (int)trim($beginning), PDO::PARAM_INT);

or if you want to stick on your existing code. 或者如果你想坚持现有的代码。

$stmt->execute(['question' => $question, 'beggining' => (int)trim($beggining)]);

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

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