SQL错误:SQLSTATE [HY093]绑定参数时

时间:2016-01-05 07:30:46

标签: php mysql pdo

嗨是使用这个pdo php类 https://github.com/indieteq/PHP-MySQL-PDO-Database-Class

我用它连接db和crud,直到现在所有工作都正常

但是当我运行此代码时不断收到此错误:SQLSTATE [HY093]:参数号无效:参数未定义

所有绑定都可以正常工作,除此之外:

    function generateRandomString()
{
    $length = 5;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i ++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    $key = $randomString;
    $setto = false;
    while ($setto == false) {
                $result = $this->db->query('SELECT `vkey` FROM `polls` WHERE `vkey`=:vkey', array(
                    'vkey' => $key
                ));
        if (count($result) > 0) {
            $randomString = '';
            for ($i = 0; $i < $length; $i ++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];
            }
            $key = $randomString;
        } else {
            $setto = true;
        }
    }
    echo $key;
    return $key;
}

我尝试获取随机搅拌并在使用该代码之前在db中检查它。

这是我的完整代码`     function __construct()     {         父:: __构建体();     }

function addpoll($args, $questions)
{
    for ($i = 0; $i < 2 + $questions; $i ++) {
        $args[$i] = parent::escape($args[$i]);
    }
    $args[1] = parent::convertHashtags($args[1]);
    $this->db->bind('title', $args[0]);
    $this->db->bind('description', $args[1]);
    $this->db->bind('answer1', $args[2]);
    $this->db->bind('answer2', $args[2]);
    $param = '';
    if ($questions > 2) {
        for ($i = 3; $i < $questions + 1; $i ++) {
            $param = $param . ':answer' . $i . ',';
            $this->db->bind('answer' . $i, $args[$i]);
        }
        if (10 - $questions > 0) {
            $count = 0;
            while (10 - $questions > $count) {
                $param = $param . ' \'\' ,';
                $count ++;
            }
        }
    } else {

        $count = 0;
        while (10 - $questions > $count) {

            $param = $param . ' \'\' ,';
            $count ++;
        }
    }

    $this->db->bind('subject', $args[$questions + 1]);

    $sql = 'INSERT INTO `polls` (`id`, `title`, `about`, `answer1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`, `answer7`, `answer8`, `answer9`, `answer10`, `mode`, `confirmation`, `who`, `password`, `confirmed`, `createdate`, `expiredate`,`subject`,`pkey`) VALUES (NULL,:title, :description ,:answer1 ,:answer2,';
    $param = $param . ' 0 , 0 , 0 , \'\' , 0 ,' . time() . ' ,';

    $settings = time() + ($args[$questions + 3] * 86400) . ',:subject , ';
    $sql = $sql . $param . $settings . '\'' . $this->generateRandomString() . '\'' . ')';
    echo $sql;

    $this->db->query($sql);
}

private function getsub($id)
{
    $result = $this->db->query('SELECT * FROM `subject` WHERE id=:sid ', array(
        'sid' => $id
    ));
    if (count($result) == 1) {
        return $id;
    }

    return 1;
}

function generateRandomString()
{
    $length = 5;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i ++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    $key = $randomString;
    $setto = false;
    while ($setto == false) {
                $result = $this->db->query('SELECT `vkey` FROM `polls` WHERE `id`=:id', array(
                    'id' => $key
                ));
        if (count($result) > 0) {
            $randomString = '';
            for ($i = 0; $i < $length; $i ++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];
            }
            $key = $randomString;
        } else {
            $setto = true;
        }
    }
    echo $key;
    return $key;
}

` 我尝试了不同的关键字,如:vkey2或..并回显所有变量 谢谢

1 个答案:

答案 0 :(得分:0)

好的,我发现了这个问题 问题在于绑定如果你使用这个类不要在执行之前尝试绑定变量只是将它们存储在数组中并且使用execute执行结束绑定我更改代码并且工作正常;)

private $binds=array();
function __construct()
{
    parent::__construct();
}

function addpoll($args, $questions)
{
    for ($i = 0; $i < 2 + $questions; $i ++) {
        $args[$i] = parent::escape($args[$i]);
    }
    $args[1] = parent::convertHashtags($args[1]);

    $this->binds['title']=$args[0];
    $this->binds['description']=$args[1];
    $this->binds['answer1']=$args[2];
    $this->binds['answer2']=$args[3];
    $param = '';
    if ($questions > 2) {
        for ($i = 3; $i < $questions + 1; $i ++) {
            $param = $param . ':answer' . $i . ',';
            $this->binds['answer' . $i]= $args[$i];
        }
        if (10 - $questions > 0) {
            $count = 0;
            while (10 - $questions > $count) {
                $param = $param . ' \'\' ,';
                $count ++;
            }
        }
    } else {

        $count = 0;
        while (10 - $questions > $count) {

            $param = $param . ' \'\' ,';
            $count ++;
        }
    }
    $this->binds['subject']=$args[$questions + 1];
    $this->binds['date']=time();
    $this->binds['todate']=time() + ($args[$questions + 3] * 86400);
    $this->binds['pkey']=$this->generateRandomString();


    $sql = 'INSERT INTO `polls` (`id`, `title`, `about`, `answer1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`, `answer7`, `answer8`, `answer9`, `answer10`, `mode`, `confirmation`, `who`, `password`, `confirmed`, `createdate`, `expiredate`,`subject`,`pkey`) VALUES (NULL,:title, :description ,:answer1 ,:answer2,';
    $param = $param . ' 0 , 0 , 0 , \'\' , 0 ,:date ,';
    echo 'test';
    $settings = ':todate ,:subject , ';
    $sql = $sql . $param . $settings . ':pkey)';

    $this->db->query($sql,$this->binds);
}

private function getsub($id)
{
    $result = $this->db->query('SELECT * FROM `subject` WHERE id=:sid ', array(
        'sid' => $id
    ));
    if (count($result) == 1) {
        return $id;
    }

    return 1;
}

function generateRandomString()
{
    $length = 5;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i ++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    $key = $randomString;
    $setto = false;
    while ($setto == false) {
                $result =  $this->db->query('SELECT * FROM `polls` WHERE pkey=:pkey ', array(
        'pkey' => $key
    ));
        if (count($result) > 0) {
            $randomString = '';
            for ($i = 0; $i < $length; $i ++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];
            }
            $key = $randomString;
        } else {
            $setto = true;
        }
    }

    return $key;
}