Jump to content

Help>> a query for category and product using mysql PDO

- - - - -

  • Please log in to reply
13 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hi guys,

i have 2 table for categorys and products

i want a query for get category names and products
only one query no more.

this is my tables:


CREATE TABLE IF NOT EXISTS `categorys` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `name` varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM;


CREATE TABLE IF NOT EXISTS `products` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `parent` int(11) unsigned NOT NULL DEFAULT '0',

  `product` varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM;


query using mysql PDO
please help


thank you

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
In the table products, the parent columns represent the id of categorys?

#3
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
yes
parent for id of category

#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
The sql code will be
select * from products p inner join categorys c on p.parent = c.id

And the PDO code can be something that look like this
$dbh = new PDO('mysql:host=;dbname=' . $dbName, $username, $password);

$sth = $dbh->prepare('select * from products p inner join categorys c on p.parent = c.id');

$sth->execute();

while ($result = $sth->fetch())

{


}

Remember, try to never use "select *", you should name the columns insted

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
SELECT `categorys`.`name`, `products`.`product` FROM `products` LEFT OUTER JOIN `categorys` ON `products`.`parent`=`categorys`.`id`;
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
True, a left join is probably better since it gonna show the categorys that don't have any product

#7
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
better is
select * from products p inner join categorys c on p.parent = c.id
OR
SELECT `categorys`.`name`, `products`.`product` FROM `products` LEFT OUTER JOIN `categorys` ON `products`.`parent`=`categorys`.`id`;

???

#8
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
The first one will won't return empty categorys (category that don't have any product)

But the second one will

Depend on what you want.

#9
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
ok tnx

i want use 'WHERE' and 'ORDER' in query, how to use?

#10
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
$sth = $dbh->prepare('select * from products p inner join categorys c on p.parent = c.id where c.name = :name order by p.product');

$sth->execute(array(':name' => 'House'));

This code will only show product from the category "House" and order it by product name
Or course you can use WingedPanther sql just add the where and the order
The reason I didn't write the category name "House" directly in the sql is that you will probably receive this information from a post or a get, and putting it in the execute function like so will prevent sql injection

#11
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
thank you vaielab :pinguin:

---------- Post added at 02:08 PM ---------- Previous post was at 01:56 PM ----------

how to create a query for 3 table?

this is my tables:


CREATE TABLE IF NOT EXISTS `categorys` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `name` varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM;


CREATE TABLE IF NOT EXISTS `products` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `parent` int(11) unsigned NOT NULL DEFAULT '0',

  `product` varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM;


CREATE TABLE IF NOT EXISTS `productsinfo` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `product_parent` int(11) unsigned NOT NULL DEFAULT '0',

  `price` varchar(255) NOT NULL DEFAULT '',

  `details` varchar(255) NOT NULL DEFAULT '',

  `images` varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM;




parent for id of category and product_parent for id of products table

thank you

#12
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Do you wish to have show the categorys when their no product in them?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users