dsq1982 2010-09-20 07:13
浏览 66
已采纳

显示SQL查询结果

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

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

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

PHP Code:

$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

alt textScreenshot 3: print_r($row)

alt text

  • 写回答

7条回答 默认 最新

  • drlq92444 2010-09-20 07:30
    关注

    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']);
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码