2010-09-20 184 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)