2017-05-09 19 views
1

我的查詢低於:使用PHP-MYSQL中的`fetch_assoc()`在預處理語句中使用`SELECT *`設置多個`WHERE`子句來設置結果的正確方法是什麼?

$query = 'SELECT * FROM `table_name` WHERE `uid` = ? AND `email` = ?'; 
$stmt_select = $conn->prepare($query); 
$stmt_select->bind_param('is', $user_id, $user_email); 
$user_id = $_SESSION['uid']; 
$user_email = $_SESSION['user_email']; 
$result = $stmt_select->execute(); 

我想打印使用fetch_assoc設置的結果,這是因爲我不想bind_result()全部23列的名字,我想它使用$row['column_name']打印,是什麼實現它的正確方法?

如果你想看到我的代碼,我在這裏問了另一個問題: How to SELECT * with multiple WHERE using fetch_assoc in prepared statements in PHP-MYSQL?

但它並沒有得到正確回答,所以不是糾正我的代碼,你能告訴我什麼是實現的最佳途徑它?

+0

我希望我的答案幫助@ user7984467 – Akintunde007

回答

0

我在mysqli中使用SELECT *也有同樣的問題。問題在於bind_result部分。通常你需要添加很多變量。意思是如果您有大約20列,則需要20個變量才能包含在bind_result聲明中。

我已經量身定製瞭解決方案來解決您的問題。

$query = 'SELECT * FROM `table_name` WHERE `uid` = ? AND `email` = ?'; 
    $stmt_select = $conn->prepare($query); 
    $stmt_select->bind_param('is', $user_id, $user_email); 
    $user_id = $_SESSION['uid']; 
    $user_email = $_SESSION['user_email']; 
    $result = $stmt_select->execute(); 
    $stmt_select->store_result();//after this line we can output the number of rows if you need to check that as well 
    $number_of_rows = $stmt_select->num_rows; 
    $meta = $stmt_select ->result_metadata(); 
    $parameters = array(); 
    $results = array(); 
    while ($field = $meta->fetch_field()) { 
     $parameters[] = &$row[$field->name]; 
    } 
    call_user_func_array(array($stmt_select , 'bind_result'), $parameters); 
     while ($stmt_select ->fetch()) { 
      foreach ($row as $key => $val) { 
        $x[$key] = $val; //here we get the key => value (Column => Value) 
         } 
         $results[] = $x; //lets grab everything here 
        } 
print_r($results); 
+0

這個按預期工作!祝你好運與你所有的未來項目! – user7984467

+0

謝謝!你也是@ user7984467。快樂編碼 – Akintunde007

相關問題