2016-01-05 40 views
0

是喜是使用PDO PHP类 https://github.com/indieteq/PHP-MySQL-PDO-Database-ClassSQL错误:SQLSTATE [HY093]同时结合PARAMS

我用它连接到数据库和污物到现在为止所有作业正常工作

但是当我运行这个代码不断收到此错误:SQLSTATE [HY093]:无效的参数号:参数没有被定义

所有绑定工作正常,除了这一点:

function generateRandomString() 
{ 
    $length = 5; 
    $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $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() { 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->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 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $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或..和呼应所有的变量 感谢

+0

您确定这是导致该错误的查询吗? 错误含义是参数的数量不等于占位符的数量。 顺便说一句,我希望'echo $ key'仅用于调试目的。 –

+0

是的,我确定。我回应所有的变量一切工作正常 –

回答

0

好吧,我发现这个问题 问题是在结合,如果你使用这个类难道没有尝试在执行之前绑定变量只是将它们存储在数组中,并执行结束绑定与执行我更改代码并且工作良好;)

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 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $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; 
} 
相关问题