【问题标题】:Display SQL query results显示 SQL 查询结果
【发布时间】:2010-09-20 07:13:10
【问题描述】:

我无法显示 SQL 查询的结果。我正在尝试显示产品表中的所有图像和价格。

我能够在浏览器中显示回显语句“Query works”。但是,结果没有显示在浏览器中。

        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
屏幕截图 2:从数据库中删除图像,并改用文件路径

屏幕截图 3:print_r($row)

【问题讨论】:

  • 您是否尝试打印使用 print_r($row) 获得的行

标签: php sql


【解决方案1】:

试试

$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']);
            }

【讨论】:

  • @Yogesh 我已经尝试了建议的各种解决方案,并且有了你的解决方案,我能够在浏览器中显示一些东西——这是一个好兆头。但现在,图像显示为字符。我尝试从数据库中删除图像,只放置文件路径,但在这两种情况下,图像都显示为字符。
  • 能否打印行并检查图像路径是否正确。
  • Array ([ProductID] => 2 [ProductDescription] => Atheletic Description. [ProductType] => Athletic [Image] => images/shoe_box.jpg [Price] => 100.00)
  • 我在执行 print_r($row) 时截取了浏览器上显示的内容。上面的评论,只是其中的一条记录,其余的都在截图中。
  • 还注意到数据库中的 9 条记录中只有 4 条正在显示。
【解决方案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;
}

你的for循环应该使用fetch_assoc作为:

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

【讨论】:

    【解决方案3】:

    $row 是查询的第一个结果行(如果有)。 $row[0] 是此查询中的第一列(由于您使用 select *,它取决于数据库中列的顺序)。所以,$row[0] > 0 是否取决于你的数据库的内容。

    【讨论】:

      【解决方案4】:

      它正在显示字符,因为这是您存储图像的方式。为了显示图像,您必须使用以下内容绘制图像:

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

      【讨论】:

        【解决方案5】:

        Mysqli 没有 fetchRow(),这是 Pear::MDB2 库的一部分

        查看文档:http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

        将循环更改为以下内容:

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

        另外,通过这样做:

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

        在循环之前,您实际上是在跳过第一行,而不是在循环中显示其图像。

        【讨论】:

          【解决方案6】:

          要打印查询的所有结果,您可以使用 while 循环

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

          【讨论】:

            【解决方案7】:

            而不是

            If ($count > 1)
            

            试试

            If ($count >= 1)
            

            【讨论】:

              猜你喜欢
              • 2018-01-19
              • 1970-01-01
              • 2012-05-02
              • 1970-01-01
              • 2011-10-02
              • 2013-12-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多