2014-02-06 63 views
0

我試圖從另一個數據庫值中使用ID獲取一些數據。這裏是我的代碼:PHP MySQLi準備語句獲取行,但不顯示數據

$query = "SELECT id,uid,product,jpgID,saledatetime,quantity 
       FROM sales 
      WHERE printed = ? 
       AND DATE_FORMAT(saledatetime, '%Y/%m/%d') = ?"; 
if ($stmt = $mysqli->prepare($query)) { 
     $printed = 0; 
     $stmt->bind_param("is",$printed,$session_date); 
     $stmt->execute(); 
     $stmt->store_result(); 

     //Get result 
     $stmt->bind_result($result_id,$result_uid,$result_product,$result_jpgID,$result_saledatetime,$result_quantity); 

     //Number of rows returned 
     $count_rows = $stmt->num_rows; 
     if ($stmt_fetch = $mysqli->prepare("SELECT jpg,udate,imageset FROM images WHERE id = ?")) { 
      //Fetch image information 
      while ($stmt->fetch()) { 
       $stmt_fetch->bind_param('i',$result_jpgID); 
       $stmt_fetch->execute(); 
       $stmt_fetch->store_result(); 
       $stmt_fetch->bind_result($jpg,$udate,$imageset); 
       echo $stmt_fetch->num_rows."<br />"; 

       //Create array's with information 
       $print[$result_product][$result_jpgID]["quantity"] = $result_quantity; 
       $print[$result_product][$result_jpgID]["location"] = $jpg; 
      } 
     } 
     $stmt->close(); 
     print_r($print); 
    } 

echo $stmt_fetch->num_rows."<br />";顯示1每次循環的時間,所以就找到了匹配的行。但對於沒有綁定的變量的一些原因,沒有任何價值......這是顯示結果:

1 
1 
1 
Array 
(
    [KR] => Array 
     (
      [137] => Array 
       (
        [quantity] => 1 
        [location] => 
       ) 

      [138] => Array 
       (
        [quantity] => 1 
        [location] => 
       ) 

     ) 

    [LR] => Array 
     (
      [138] => Array 
       (
        [quantity] => 1 
        [location] => 
       ) 

     ) 

) 
+1

並且您完全確定查看jpg,udate,imageset的第二個查詢實際上與手動運行時提供的ID一起使用?即。在phpmyadmin或控制檯?另外,對原始查詢(我甚至不知道)進行連接並不容易,然後只是循環一個結果集,而不是將這2個結合起來使用 – Tularis

+0

@Tularis,嗯它不運行如果我手動定義標識..:S這是我知道這樣做的唯一途徑,我將如何結合這些查詢呢? –

+0

還添加了第一個查詢。 –

回答

0

設法解決它,這是我固定的代碼。

if(login_check($mysqli) == true) { 
    //logged in 
    $session_date = $_SESSION['session_date']; 

    $query = "SELECT id,uid,product,jpgID,saledatetime,quantity 
       FROM sales 
      WHERE printed = ? 
       AND DATE_FORMAT(saledatetime, '%Y/%m/%d') = ?"; 
    if ($stmt = $mysqli->prepare($query)) { 
     $printed = 0; 
     $stmt->bind_param("is",$printed,$session_date); 
     $stmt->execute(); 
     $stmt->store_result(); 
     $stmt->bind_result($result_id,$result_uid,$result_product,$result_jpgID,$result_saledatetime,$result_quantity); 
     $count_rows = $stmt->num_rows; 

     while ($stmt->fetch()) { 
      //Create array's with information 
      $print[$result_product][$result_jpgID]["quantity"] = $result_quantity; 
     } 
     $stmt->close(); 

     foreach ($print as $key1 => $value1) { 
      foreach ($print[$key1] as $key => $value) { 
       if ($stmt = $mysqli->prepare("SELECT jpg,udate,imageset FROM images WHERE id = ?")) { 
       $stmt->bind_param('i',$key); 
        $stmt->execute(); 
        $stmt->store_result(); 
        $stmt->bind_result($jpg,$udate,$imageset); 
       } 
       while ($stmt->fetch()) { 
        $print[$key1][$key]['location'] = $jpg; 
       } 
      } 
     } 
     print_r($print); 
    } 
} else { 
    header("Location: index.php"); 
}