简体   繁体   English

Phalcon:异常:参数数量错误

[英]Phalcon : Exception: Wrong number of parameters

I want to execute a prepared insert:我想执行一个准备好的插入:

<?php
function add_record($tab) {

        $champs= "";
        $value = "";
        $separateur ="";
        $ins = array();

        foreach ($tab as $k => $v){
            $champs .= $separateur . $k;
            $value .= $separateur . "?" ;
            $ins[] = $v;
            $separateur = ",";
        }
        $champs = '('.$champs.')';
        $value = '('.$value.')';

        $sSQL = "INSERT INTO facture $champs VALUES $value";

        executePreparedDML($sSQL, $ins);

    }

    function executePreparedDML($sSQL, $tab) {
    $config = array(
        "host" => SERVEUR,
        "dbname" => BDD,
        "port" => BDD_PORT,
        "username" => LOGIN_DB,
        "password" => PWS_DB);
    $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config);
    $statement = $connection->prepare($sSQL);
    $pdoResult = $connection->executePrepared($statement, $tab);
}
?>

When executing the add_record function then I got this error: Exception: Wrong number of parameters?执行 add_record function 时出现此错误:异常:参数数量错误? So what is wrong ?那么有什么问题呢?

According to manual : 根据手册

public PDOStatement executePrepared (PDOStatement $statement, array $placeholders, array $dataTypes) 公共PDOStatement executePrepared(PDOStatement $ statement,数组$ placeholders,数组$ dataTypes)

Executes a prepared statement binding. 执行准备好的语句绑定。 This function uses integer indexes starting from zero 此函数使用从零开始的整数索引

In other part of manual we can see that Phalcon allows bindings of names eg. 在手册的其他部分 ,我们可以看到Phalcon允许绑定名称,例如。 = :email or bindings indexed as of ?0, ?1 . = :email?0, ?1索引的= :email或绑定。

As you function is serving array of [0 => 'first', 1 => 'second'] , i would test to create chain like (?0, ?1, ?2, ?3, ?4) instead of (?,?,?,?,?) . 当您的函数正在提供[0 => 'first', 1 => 'second']数组时,我会测试创建类似(?0, ?1, ?2, ?3, ?4)(?0, ?1, ?2, ?3, ?4)而不是(?,?,?,?,?)

You can also try to not prepare it the way you are doing it in question example and go straight for version from your own answer. 您也可以尝试以问题示例中的方式进行准备,并直接从自己的答案中获取版本。

Also, when using models it is as easy as: 另外,使用模型时,它很容易:

$obj = new credentialModel();
$isOk = $obj->save(array(
    'name' => 'John',
    'surname' => 'Doe'
));

and once you are using Phalcon, I would recommend to actually use it. 一旦您使用了Phalcon,我建议您实际使用它。

I wrote the sql manually but not dynamically : 我手动编写了sql,但没有动态编写:

$tab["fact_date"] = convertDateFormat5($tab["fact_date"]);

        $config = array(
        "host" => SERVEUR,
        "dbname" => BDD,
        "port" => BDD_PORT,
        "username" => LOGIN_DB,
        "password" => PWS_DB);

        $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config);

        $sSQL = "insert into facture(user_code,clt_id,fact_ref,fact_lib,fact_date,fact_comm) values(?,?,?,?,?,?)";

        $statement = $connection->execute($sSQL, array($tab['user_code'], $tab['clt_id'], $tab['fact_ref'], $tab["fact_lib"], $tab['fact_date'], $tab["fact_comm"]));
        $id = $connection->lastInsertId();
        $connection->close();

        return $id;

With this approach it works , so I dont understand why it doesnt when written dynamically ! 通过这种方法,它可以工作,所以我不明白为什么动态编写时不起作用!

just go to app/config/router.php and replace this line of code $router->handle() with $ router->handle($_SERVER['REQUEST_URI'])只需 go 到app/config/router.php并将这行代码$router->handle()替换为 $ router->handle($_SERVER['REQUEST_URI'])

It's a bit round about way of doing things, but considering all else in code is good, ie the $tab values, etc. the error is see is... 它的处理方式有点复杂,但是考虑到代码中的所有其他内容都很好,例如$tab值,等等。看到的错误是...

$sSQL = "INSERT INTO facture $champs VALUES $value"; should be $sSQL = "INSERT INTO facture " .$champs. " VALUES ". $value; 应该是$sSQL = "INSERT INTO facture " .$champs. " VALUES ". $value; $sSQL = "INSERT INTO facture " .$champs. " VALUES ". $value;

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

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