php 显示 SQL 查询结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3749329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:58:17  来源:igfitidea点击:

Display SQL query results

phpsql

提问by jc70

I am having trouble displaying results from a SQL query. I am trying to display all images and prices from a products table.

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

I am able to display the echo statement "Query works" in the browser. But, the results are not displaying in the browser.

我能够在浏览器中显示 echo 语句“查询有效”。但是,结果没有显示在浏览器中。

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

PHP Code:

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

}

}

Screenshot 1
alt textScreenshot 2: removed the images from the database, and used a filepath instead

截图 1
替代文字截图 2:从数据库中删除图像,并使用文件路径代替

alt textScreenshot 3: print_r($row)

替代文字截图 3:print_r($row)

alt text

替代文字

回答by codaddict

I think

我认为

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

should be

应该

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

And your for loop should use fetch_assocas:

你的 for 循环应该fetch_assoc用作:

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

回答by Nik

try

尝试

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

回答by Alex

It's displaying characters because that is how you have stored the image. In order to show the image you are going to have to draw the image with something like:

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

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

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

回答by Jochem

$row is the first result-row (if any) from your query. $row[0] is the first column in this query (which, since you use select *, depends on the order of the columns in your database). So, whether $row[0] > 0 depends on the content of your database.

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

回答by Fanis Hatzidakis

Mysqli doesn't have fetchRow(), that's part of the Pear::MDB2 library

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

See the docs: http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

请参阅文档:http: //www.php.net/manual/en/mysqli-result.fetch-assoc.php

Change your loop to the following:

将您的循环更改为以下内容:

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

Also, by doing this:

此外,通过这样做:

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

before the loop you are essentially skipping the first row and not displaying its image in the loop.

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

回答by In81Vad0

to print all results from a query you can use a while loop

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

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

回答by user1932459

Instead of

代替

If ($count > 1)

Try

尝试

If ($count >= 1)