2012-10-30 86 views
0

這是我的類代碼:的mysqli - mysqli_stmt :: bind_param()預計將參考

class DAL 
{ 

public function paramtypez($val) 
{ 
     $types = '';      //initial sting with types 
     foreach($val as $para) 
     {  
      if(is_int($para)) { 
       $types .= 'i';    //integer 
      } elseif (is_float($para)) { 
       $types .= 'd';    //double 
      } elseif (is_string($para)) { 
       $types .= 's';    //string 
      } else { 
       $types .= 'b';    //blob and unknown 
      } 
     } 
     return $types; 
} 

public function execsql($query,$param) 
{ 
    $conn=new mysqli("127.0.0.1","root","","sample"); 

    if($row=$conn->prepare($query)) 
    { 
     array_unshift($param,array(DAL::paramtypez($param))); 
     call_user_func_array(array($row, 'bind_param'), $param); 

     $row->execute(); 
     return true; 
    } 
    else 
    return false; 

} 
} 

,並使用這個類是這樣的:

$vars = new DAL(); 
$vars->execsql('insert into tesst (name,family,age) values(?,?,?)',array('mori','gre','25')); 

但是這回我這個錯誤:

Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ..\htdocs\inc.php on line 30 

如何解決這個錯誤? 感謝您的幫助

回答

2

$param看起來像你之前這個權利將其傳遞給call_user_func_array:

[['ssi'],'mori','gre','25'] 

這是不正確的。如果你想改變:

array_unshift($param,array(DAL::paramtypez($param))); 

要這樣:

array_unshift($param,DAL::paramtypez($param)); 
2

發生這種情況,因爲你給一個數組時bind_param預計實際參考:

嘗試,看看,如果這個工程:

class DAL 
{ 

    public function paramtypez($val) 
    { 
      $types = '';      //initial sting with types 
      foreach($val as $para) 
      {  
       if(is_int($para)) { 
        $types .= 'i';    //integer 
       } elseif (is_float($para)) { 
        $types .= 'd';    //double 
       } elseif (is_string($para)) { 
        $types .= 's';    //string 
       } else { 
        $types .= 'b';    //blob and unknown 
       } 
      } 
      return $types; 
    } 

    public function execsql($query,$param) 
    { 

     $conn=new mysqli("127.0.0.1","root","","sample"); 

     if($row=$conn->prepare($query)) 
     { 
      array_unshift($param,array(DAL::paramtypez($param))); 
      call_user_func_array(array($row, 'bind_param'), $this->makeValuesReferenced($param)); 

      $row->execute(); 
      return true; 
     } 
     else 
     return false; 

    } 


    protected function makeValuesReferenced(&$arr) 
    { 
     $refs = array(); 
     foreach($arr as $key => $value) 
      $refs[$key] = &$arr[$key]; 
     return $refs; 
    } 

} 
+0

謝謝,但...警告:mysqli_stmt :: bind_param()預計參數1是字符串數組中inc.php給出上線31 – Nulled

+0

你可能想看看http://ca.php.net/manual/en/mysqli-stmt.bind-param.php#96770 –

相關問題