2014-06-16 2 views
0

MySql에 대한 데이터 연결을 설정하고 준비된 문을 작성하려고합니다. 여기서 query_f 함수는 첫 번째 매개 변수가 SQL 문이고 다른 매개 변수는 값입니다 그것은 준비된 진술에서 대체 될 것입니다.PHP mysqli_bind_param 함수 문제. 준비된 문 구현하기

다음은 내가 가지고있는 것입니다. 첫 번째 오류는 값을 문에 바인딩하려고 할 때 발생합니다.

function query_f(/* query, [...] */){ 
    $user = "root"; 
    $pass = "root"; 
    $host = "localhost"; 
    $database = "mcnair"; 
    $conn = mysqli_connect($host,$user,$pass); 
    if(!$conn) 
    { 
     echo "Cannot connect to Database"; 
    } 
    else 
    { 
     mysqli_select_db($conn, $database); 
    } 

    // store query 
    $query = func_get_arg(0); 
    $parameters = array_slice(func_get_args(), 1); 
    $param = "'".implode("','",$parameters)."'"; 

    // Prepare the statement 
    $stmt = mysqli_prepare($conn, $query); 
    if ($stmt == false) 
    { 
     echo "The statement could not be created"; 
     exit; 
    } 

    // Bind the parameters 
    $bind = mysqli_stmt_bind_param($stmt, 's', $param); 
    echo mysqli_stmt_error($stmt); 
    if ($bind == false) 
    { 
      echo "Could not bind"; 
    } 
    else 
    { 
     echo "Bind successful"; 
    } 

    // Execute the statement 
    $execute = mysqli_stmt_execute($stmt); 
    if ($execute = false) 
    { 
     echo "Could not execute"; 
    } 



    // fetch the data 
    $fetch = mysqli_stmt_fetch($stmt) 
    if ($fetch == false) 
    { 
      echo "Could not fetch data"; 
    } 
    else 
    { 
     return $fetch; 
    } 
} 

그리고 내가 사용하고 함수 호출은 다음과 같습니다

query_f("SELECT Hash FROM alumni WHERE Username = '?'", "zm123"); 
+2

"첫 번째 오류가 발생했습니다 ..."오류가 있습니까? –

+0

사용자 지정 함수를 작성하기 전에 원시 API로 연습해야합니다. –

+2

'Username = '?''<='? ' –

답변

1

하는 방법 (대신 함수의) 클래스를 사용하고 OO 방식으로가 아니라 절차 적 방법으로 mysqli를 사용하는 방법에 대한?

이것은 내가 사용하는 단순화 된 버전입니다. 완벽하지 않기 때문에 개선을 제안하고자하는 사람은 누구나 귀에 있습니다.

class Connection { 

    private $connection; 

    public function __construct() 
    { 
     //better yet - move these to a different file 
     $dbhost = ''; 
     $dbuname = ''; 
     $dbpass = ''; 
     $dbname = ''; 

     $this->connection = new mysqli($dbhost, $dbuname, $dbpass, $dbname); 
    } 

    /* 
    * This is the main function. 
    * 
    * @param $arrayParams = array (0 => array('s' => 'Example string'), 1 => array('s' => 'Another string'), 2 => array('i' => 2), 3 => array('d' => 3.5)) 
    */ 
    public function executePrepared($sql, $arrayParams) 
    { 
     $statement = $this->prepareStatement($sql); 
     if ($statement) { 
      $this->bindParameter($statement, $arrayParams); 
      $this->executePreparedStatement($statement); 
      $result = $this->getArrayResultFromPreparedStatement($statement); 
      //only close if you are done with the statement 
      //$this->closePreparedStatement($statement); 
     } else { 
      $result = false; 
     } 

     return $result; 
    } 

    public function prepareStatement($sql) 
    { 
     $statement = $this->connection->prepare($sql) or $this->throwSqlError($this->connection->error); 
     return $statement; 
    } 

    public function bindParameter(&$statement, $arrayTypeValues) 
    { 
     $stringTypes = ''; 
     $arrayParameters = array(); 
     $arrayParameters[] = $stringTypes; 
     foreach ($arrayTypeValues as $currentTypeVale) { 
      foreach ($currentTypeVale as $type => $value) { 
       $stringTypes .= $type; 
       $arrayParameters[] = &$value; 
      } 
     } 
     $arrayParameters[0] = $stringTypes; 

     call_user_func_array(array($statement, "bind_param"), $arrayParameters); 
    } 

    public function getArrayResultFromPreparedStatement(&$statement) 
    { 
     $statement->store_result(); 

     $variables = array(); 
     $data = array(); 
     $meta = $statement->result_metadata(); 

     while($field = $meta->fetch_field()) 
      $variables[] = &$data[$field->name]; // pass by reference 

     call_user_func_array(array($statement, 'bind_result'), $variables); 

     $i = 0; 
     $arrayResults = array(); 
     while($statement->fetch()) 
     { 
      $arrayResults[$i] = array(); 
      foreach($data as $k=>$v) 
      { 
       $arrayResults[$i][$k] = $v; 
      } 
      $i++; 
     } 

     return $arrayResults; 
    } 

    public function executePreparedStatement($statement) 
    { 
     $result = $statement->execute() or $this->throwSqlError($statement->error); 
     return $result; 
    } 

    public function closePreparedStatement($statement) 
    { 
     $statement->close(); 
    } 

    public function throwSqlError() 
    { ... } 

}