tags:

views:

98

answers:

6

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 text Screenshot 2: removed the images from the database, and used a filepath instead

alt text Screenshot 3: print_r($row)

alt text

+1  A: 

$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.

Jochem
+2  A: 

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_assoc as:

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

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

See the docs: 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.

Fanis
A: 

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

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

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']);
            }
Yogesh
@Yogesh i've tried the various solutions suggested, and with yours i was able to display something in the browser--which is a good sign. but right now, the images are showing up as characters. i tried removing the images from the database, and just putting the file path, but in both cases the images display as characters.
jc70
Can you print the row and check whether image path is coming properly or not.
Yogesh
Array ( [ProductID] => 2 [ProductDescription] => Atheletic Description. [ProductType] => Athletic [Image] => images/shoe_box.jpg [Price] => 100.00 )
jc70
I took a screenshot of what is displayed on my browser when I did print_r($row). The comment above, is only one of the records, the rest are in the screenshot.
jc70
Also noticed that only 4 out of the 9 records in the database are displaying.
jc70
It seems that you have changed datatype from blob to varchar/text. With that last screenshot , paths are coming proper. Just make sure to use absolute path than relative path. currently i guess you are using relative path. If so, just ad necessary path before image . Due to ralative path you are unable to get images or otherwise make sure your relative path is correct.
Yogesh
+1  A: 

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

Alex