Hello. I have a sorting list which is populated with the different "types" and when a "type" is chosen the images along with corresponding info should be displayed but instead the page is blank with no error. I have another sorting which is more or less the same for sorting price/ascending/descending and these gets displayed but like i said the coding is sort of the same so i dont get why its not working.

this list

<?php 
  $stmt = $conn->prepare("SELECT DISTINCT type FROM tbl");
  $stmt->execute();
  $result = $stmt->fetchAll();
?>
<ul class="dropdown-menu" style="width: 100%;">
<?php foreach($result as $readrow) { ?>
   <li><a href="test.php?type=<?php echo $readrow['fld_product_type']; ?>"><?php echo $readrow['fld_product_type']; ?></a></li>                  <?php
   }
   $conn = null;
?>
</ul>

the select and html:

if(isset($_GET["type"])){
   $gettype = $_GET["type"];
   $stmt = $conn->prepare("SELECT * FROM tbl WHERE type = :cat");
   $stmt->bindParam(':cat', $gettype, PDO::PARAM_STR);
   $stmt->execute();
   $result = $stmt->fetchAll();
?>

<?php foreach($result as $readrow) { ?>
  <div class="col-sm-4 col-lg-4 col-md-4">
     <div class="thumbnail" >
         <?php if ($readrow['img'] == "" ) { ?>
            <img src="products/noimage.jpg" class="img-responsive">
              <?php } else { ?>
                <img src="products/<?php echo $readrow['img'] ?>" class="img-responsive">
              <?php } ?>

              <div class="caption" >
                <h3><?php echo $readrow['id']; ?></h3>
                <p style="height: 90px;">
                  <strong>Name : </strong><?php echo $readrow['name']; ?><br>
                  <strong>Price :</strong> RM<?php echo $readrow['price']; ?><br>
                  <strong>Brand : </strong> <?php echo $readrow['type']; ?><br>
                </p>
              </div>
        <a href="products_details.php?pid=<?php echo $readrow['id']; ?>" class="btn btn-primary btn-block" role="button">View</a>
      </div>
   </div>
<?php } $conn = null; ?>

so the html bit is to display so sorting and all other else uses it. the only difference is the select and list part. the select statements are in if elseif statements.

Dani AI

Generated

A blank page with no error usually means either a hidden PHP error or simply no output because the queries returned nothing. was right to flag the potential for reserved-word trouble, but since later confirmed the column is actually named fld_product_type the immediate suspects are: a column-name mismatch between what you SELECT and what you echo, the WHERE using the wrong column, or the DB handle being closed before the filtering query runs.

Quick, practical debugging steps you can apply right now:

// turn on errors for debugging
ini_set('display_errors', 1);
error_reporting(E_ALL);

// make PDO throw exceptions so you see failures
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// after the query, inspect the result
var_dump($rows); // or error_log(print_r($rows, true));

Run those at the top of the script and immediately after each fetch to see exactly what the database returned and whether $_GET['type'] actually contains the value you expect.

Fixes and checklist

  • Make the column name consistent everywhere: the column you SELECT must be the same name you use when building the link and in the WHERE clause.
  • Don’t null/close the connection until after all queries have run. Closing the PDO handle early will break later queries.
  • Protect URL values with urlencode() so spaces or special characters don’t break the GET parameter.
  • Use fetchAll(PDO::FETCH_ASSOC) (or inspect the fetch mode) so you get associative keys you expect.
  • If you truly have a column called type, quote it with backticks, not single quotes (single quotes are string literals).
  • If output still doesn’t appear, check the HTML source or browser inspector to see if the markup is present but hidden by CSS, and check PHP/Apache error logs for fatal errors.

Following those steps should either reveal the exact error message or immediately restore expected results.

Recommended Answers

All 2 Replies

Type is a reserved word in MySql so this line

$stmt = $conn->prepare("SELECT DISTINCT type FROM tbl");

will be failing. You need to quote the word type for it to be treated as a column name.

$stmt = $conn->prepare("SELECT DISTINCT 'type' FROM tbl");

actually its fld_product_type, so the reserved word ides doesn't apply. though thats a tiny bit info i didn't know. you learn new things everyday! thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.