Jump to content

PHP SELECT CODE HELP!


lill77

Recommended Posts

Hello Im trying to find the best way to pull in products by category into my page. i have the following code is this the best way or correct ? thanks

 

<?php
include 'admin/db.php'; 

include 'header-home.php';

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");


// create query
$query = "SELECT * FROM `category` ORDER BY 'CAT' ASC";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
     
    while($row = mysql_fetch_array($result)) {
          echo "<tr><td align='left' valign='top'><a href='search.php?cat=$row[iD]'>$row[CAT]</a> </tr>";



    }
  
}
else {
    // no
    // print status message
    echo "<p align='center'><font color='#808080' face='Arial'>No records found.</font></p>";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

Link to comment
Share on other sites

What is/is not happening compared to what you want to happen? You state you want a list of products, but it appears the query is only pulling the categories. Although I see what appears to be an error in that.

 

There are strait, single quotes around the field name in the ORDER BY parameter. I'm pretty sure it should be using the slanted quotes.

$query = "SELECT * FROM `category` ORDER BY `CAT` ASC";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.