2013-03-28 63 views
1

我有一個MySQL查詢,我正在將它轉換爲mysqli(準備語句),但我遇到了問題會拋出下面的錯誤,警告:mysqli_stmt :: bind_result():綁定變量的數量不匹配準備語句中的字段數量錯誤

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement 

MySQL代碼

$random_name_generated = vpb_generate_random_name().'.jpg'; //Generated name for uploaded files or images 

        if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) 
        { 
        $check_empty_field = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'"); 
        if(mysql_num_rows($check_empty_field) < 1) 
        { 
        mysql_query("insert into `vpb_uploads` values('', '".mysql_real_escape_string($username)."', '', '', '".mysql_real_escape_string($random_name_generated)."', '', '', '', '', '".mysql_real_escape_string(date("d-m-Y"))."')"); 

        $identity = "image_one"; 
        } 
        else 
        { 
        $get_empty_field = mysql_fetch_array($check_empty_field); 
        $image_one = strip_tags($get_empty_field["image_one"]); 
        $image_two = strip_tags($get_empty_field["image_two"]); 
        $image_three = strip_tags($get_empty_field["image_three"]); 
        $image_four = strip_tags($get_empty_field["image_four"]); 
        $image_five = strip_tags($get_empty_field["image_five"]); 
        global $identity; 

下面是我的嘗試,即使它沒有工作。我已經知道這是行不通的,但我想在問一個問題之前親自嘗試。而錯誤來自$get_empty_field = $stmt->bind_result($stmt);任何人都可以幫我弄清楚出了什麼問題?

if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) 
       { 
       $firstname = '""'; 
       $lastname = '""'; 
       $stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?"); 
       $stmt->bind_param('sss', $username, $firstname, $lastname); 
       $stmt->execute(); 
       $stmt->store_result(); 
       if ($stmt->num_rows < 1) 

       { 
       $date = 'date("d-m-Y")'; 
       $image_2 = "''"; 
       $image_3 = "''"; 
       $image_4 = "''"; 
       $image_5 = "''"; 
       $stmt = $mysqli->prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, `image_one`, `image_two`, `image_three`, `image_four`, `image_five`, `date`) values(?,?,?,?,?,?,?,?,?)"); 
       $stmt->bind_param('sssssssss', $username, $firstname, $lastname, $random_name_generated, $image_2, $image_3, $image_4, $image_5, $date); 
       $stmt->execute(); 
       $stmt->close(); 

       $identity = "image_one"; 
       } 
       else 
       { 
       $get_empty_field = $stmt->bind_result($stmt); 
       $image_one = strip_tags($get_empty_field["image_one"]); 
       $image_two = strip_tags($get_empty_field["image_two"]); 
       $image_three = strip_tags($get_empty_field["image_three"]); 
       $image_four = strip_tags($get_empty_field["image_four"]); 
       $image_five = strip_tags($get_empty_field["image_five"]); 
       global $identity; 

回答

7

您需要更改

$get_empty_field = $stmt->bind_result($stmt); 

$get_empty_field = $stmt->bind_result($field1, $field2, $field3); 

$fieldx變量的數量等於被選定字段數。如果你不知道有多少,使用此:

// Throw an exception if the result metadata cannot be retrieved 
    if (!$meta = $stmt->result_metadata()) 
    { 
    throw new Exception($stmt->error); 
    } 

    // The data array 
    $data = array(); 

    // The references array 
    $refs = array(); 

    // Iterate over the fields and set a reference 
    while ($name = $meta->fetch_field()) 
    { 
    $refs[] =& $data[$name->name]; 
    } 

    // Free the metadata result 
    $meta->free_result(); 

    // Throw an exception if the result cannot be bound 
    if (!call_user_func_array(array($stmt, 'bind_result'), $refs)) 
    { 
    throw new Exception($stmt->error); 
    } 

然後你訪問結果,獲取,使用$data['field']後;

+0

我改變了你所說的,但它仍然引發了警告。 – magnsta

+0

表中有多少個字段?你有多少變量傳遞給'bind_result'函數? – MichaelRushton

+0

表中有9個字段,我傳遞了3個變量。 – magnsta

相關問題