Error: number of bound variables does not match number of tokens

我们两清 提交于 2019-12-01 10:52:30

问题


I want to make an insert only if there's no correspondence in the db (mySQL) but he makes me not the statement. Here's the snippet

    if ($sql->rowCount() > 0) {
        echo 'Non inserisci';
    } else {
        echo 'Inserisci';
        $db->beginTransaction();
        echo 'Ciao3';
        $sql = $db->prepare("INSERT INTO contatti (nome,cognome) VALUES (?,?)") or die('Ciao2');
        echo 'Ciao4';
        $sql->execute(array($_POST['nome'],$_POST['cognome']));
        echo 'Ciao5';
        $db->rollBack();
    }

Where The SELECT is

    $db->beginTransaction();
    $sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');
    $sql->execute(array($_POST['nome'],$_POST['cognome']));
    $db->rollBack();

Can you explain me where's the fault?


回答1:


The fault is in arithmetics

Let's count tokens:

 SELECT * FROM contatti WHERE nome = ? // one
                       AND cognome = ? // two
                        WHERE nome = ? // three
                       AND cognome = ? // four

now let's count number of bound variables:

array($_POST['nome'], // one
      $_POST['cognome']) // two

4 is apparently not equal to two. that's the problem




回答2:


It seems you have done a little too much copy/paste here:

$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');

should probably just be

$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ?") or die ('Ciao1');

You have had the WHERE clause doubled.



来源:https://stackoverflow.com/questions/16108459/error-number-of-bound-variables-does-not-match-number-of-tokens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!