2010-09-20 180 views
1

我無法顯示來自SQL查詢的結果。我正試圖顯示產品表中的所有圖像和價格。顯示SQL查詢結果

我能夠在瀏覽器中顯示「Query works」的echo語句。但是,結果並未在瀏覽器中顯示。

 if ($count > 0) { 
      echo "Query works"; 
     } else { 
      echo "Query doesn't work" ."<br/>"; 
     } 

PHP代碼:

$con = getConnection(); 
     $sqlQuery = "SELECT * from Products"; 

     // Execute Query -----------------------------   
     $result = mysqli_query($con, $sqlQuery); 
      if(!$result) { 
       echo "Cannot do query" . "<br/>"; 
       exit; 
      } 

      $row = mysqli_fetch_row($result); 
      $count = $row[0]; 

      if ($count > 0) { 
       echo "Query works"; 
      } else { 
       echo "Query doesn't work" ."<br/>"; 
      } 

      // Display Results ----------------------------- 

      $num_results = $result->numRows(); 

      for ($i=0; $i<$num_results; $i++) { 
       $row = $result->fetchRow(MDB2_FETCH_ASSOC); 
       echo '<img src="'.$row['Image'].'>'; 
       echo "<br/>" . "Price: " . stripslashes($row['Price']); 

}

截圖1個
alt text 截圖2:除去來自數據庫中的圖像,以及所使用的文件路徑,而不是

alt text 截圖3:的print_r($行)

alt text

+0

你嘗試打印你用print_r得到的行($ row) – svk 2010-09-20 07:24:07

回答

2

嘗試

$sqlQuery = "SELECT * from Products"; 

     // Execute Query -----------------------------   
     $result = mysqli_query($con, $sqlQuery); 
      if(!$result) { 
       echo "Cannot do query" . "<br/>"; 
       exit; 
      } 

      $row = mysqli_fetch_row($result); 
      $count = $row[0]; 

      if ($count > 0) { 
       echo "Query works"; 
      } else { 
       echo "Query doesn't work" ."<br/>"; 
      } 

      // Display Results ----------------------------- 

      $num_results =mysqli_num_rows($result); 

      for ($i=0; $i<$num_results; $i++) { 
       $row = mysqli_fetch_assoc ($result); 
       //print_r($row); 
       echo '<img src="'.$row['Image'].'>'; 
       echo "<br/>" . "Price: " . stripslashes($row['Price']); 
      } 
+0

@Yogesh我已經嘗試了各種解決方案,並且你可以在瀏覽器中顯示一些東西 - 這是一個很好的選擇IGN。但現在,圖像顯示爲字符。我試圖從數據庫中刪除圖像,只是放置文件路徑,但在這兩種情況下圖像顯示爲字符。 – jc70 2010-09-20 08:00:05

+0

您可以打印行並檢查圖像路徑是否正確到達。 – Nik 2010-09-20 08:12:05

+0

Array([ProductID] => 2 [ProductDescription] => Atheletic Description。[ProductType] => Athletic [Image] => images/shoe_box.jpg [Price] => 100.00) – jc70 2010-09-20 08:32:31

1

$行是第一個結果行(如果有的話)查詢。 $ row [0]是此查詢中的第一列(這是因爲您使用select *,取決於數據庫中列的順序)。所以,$ row [0]> 0是否取決於數據庫的內容。

2

我覺得

$row = mysqli_fetch_row($result); 
$count = $row[0]; 

應該

$count = $result->numRows(); 
if ($count > 0) { 
    echo "Query produced $count rows"; 
} else { 
    echo "Query produced no rows" ."<br/>"; 
    return; 
} 

和你的循環應該使用fetch_assoc爲:

while ($row = $result->fetch_assoc()) { 
    echo '<img src="'.$row['Image'].'>'; 
    echo "<br/>" . "Price: " . stripslashes($row['Price']); 
} 
0

的mysqli沒有fetchRow(),那是梨的一部分:: MDB2文庫

請參閱該文檔:做這個

while ($row = $result->fetch_assoc()) { 
    echo '<img src="'.$row['Image'].'>'; 
    echo "<br/>" . "Price: " . stripslashes($row['Price']); 
} 

而且,:

$row = mysqli_fetch_row($result); 
$count = $row[0]; 

循環之前你基本上是跳過第一行,而不是http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

更改你的循環以下在循環中顯示其圖像。

0

打印所有結果從查詢,你可以使用while循環

while($row=mysqli_fetch_assoc($result)){ 
    echo 'Price '.$row['Price'].'<br/>'; 
} 
1

它的顯示字符,因爲那是你如何保存圖像。爲了顯示圖像,你將不得不繪製圖像的東西,如:

echo '<img src="data:image/gif;base64,'.base64_encode($row['Image']).'" />';

0

而不是

If ($count > 1) 

嘗試

If ($count >= 1)