Displaying 2 "rows" from database

I hope I explain this right. I am trying to learn and I am wanting to pull 2 “rows” from my database.

For example, I have items that have categories.

Cars
Trucks
SUV’s

Then I have condition

New
Used
Parts only

I would I display multiple items on the page that are New Trucks or Parts Only Cars?

Sorry for the noob question, just trying to learn.

Please supply your database table structures:

If you are using http://localhost/phpmyadmin the table structures can be shown by:

  1. selecting the table
  2. select: export
  3. select: Custom - display all possible options
  4. select: View output as text
  5. select: structure
  6. Go
CREATE TABLE IF NOT EXISTS `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `btype` int(8) NOT NULL DEFAULT '4',
  `type` varchar(127) NOT NULL,
  `title` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `xrl` varchar(128) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `xrl` (`xrl`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;

There are many Tutorials on selecting and displaying data, try the following search:

Google: “tutorial PHP mysql_query()”

The mysql_* extension is deprecated as of version 5.5 and is being removed as of version 7 of PHP (which is due to come out later this year). @laflair13 you’re better off googling for tutorials that use PDO

1 Like

The question is how to construct SQL query? Or how to execute it from PHP? Or both?
Clarify it please. I can help with the answer but i don’t want to do useless work answering the wrong question

Below is thew structure I have.

-- phpMyAdmin SQL Dump
-- version 4.3.8
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 13, 2015 at 08:00 AM
-- Server version: 5.5.40-36.1
-- PHP Version: 5.4.23

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Database: `tmcguire_database`
--

-- --------------------------------------------------------

--
-- Table structure for table `new_equip`
--

CREATE TABLE IF NOT EXISTS `new_equip` (
  `id` int(11) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `itemname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `desc` varchar(12000) COLLATE utf8_unicode_ci NOT NULL,
  `condition` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `category` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `image` longblob,
  `owner` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

) ENGINE=MyISAM AUTO_INCREMENT=235 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `new_equip`
--
ALTER TABLE `new_equip`
  ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `id` (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `new_equip`
--
ALTER TABLE `new_equip`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=235;

And what I am trying to do is display the item on my frontend based on Category and Condition. Like New Car, or Used Truck. I know how to do it with just one. I use the code below to do that.

include_once('mysql_connect.php');
$Amt = 20;//Amount per page
$Cat = $_GET['Cat'];
  $Page = @$_GET['Page'];
$SQLCat = "";
if (strlen($Cat) > 1)
{
$SQLCat = "AND (`Category` LIKE '$Cat;%' OR `Category` LIKE '%;$Cat' OR `Category` LIKE '$Cat')";	
}
$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE (`condition`='New' OR `condition`='Excellent' OR `condition`='Very Good') $SQLCat $Limit";
$R_GetEquipment = mysql_query($SQL_GetEquipment, $Link);

Do you want to just output everything but ordered by the category? so a list of New followed by Used followed by Parts only?

You can just put ORDER BY type at the end of your query if so.

It may just be an example and you haven’t done it to keep the code clean but you must escape any string you use in a query. For mysqli it would be

$cat = mysqli_real_escape_string($connection, $cat);

Otherwise you are leaving your database open to attack

I’m still not sure i’m getting it right.
You want to filter items with category and condition?
As i see you have already done categories filter.
So why can’t you just add another variable and put in into your query?

I mean something like this:

$cond = $_GET['cond'];
$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE (`condition`='$cond') $SQLCat $Limit";

PS: Are you store categories data in a single field? That’s very bad practice. You should use one-to-many relation. Learn about it here.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.