Jump to content

Recommended Posts

hello there, i need some help to display some items from my articles table, the articles table is related whit the other table of mine called category, and in the table categorys there are on column that is called category, inside there are 3 type, and one of them that cals "profile" is the one i need, i need the display all of my articles that is related whit the table categorys and column named "profile", above is the original query

 

Original query:

$sql = "SELECT * FROM articles order by articles.id DESC LIMIT ".$from.", 20";

 

Query i tryed to change:

$sql = "SELECT * FROM articles WHERE category = profile and order by articles.id DESC LIMIT ".$from.", 20";

 

I cant put this work i tryed many thign but no suscess, could someone help me?

 

Could someone help me

Link to comment
https://forums.phpfreaks.com/topic/269091-php-query-dont-work/
Share on other sites

CREATE TABLE IF NOT EXISTS `categorys` (

`category` varchar(200) NOT NULL,

PRIMARY KEY (`category`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

--

-- Dumping data for table `categorys`

--

 

INSERT INTO `categorys` (`category`) VALUES

('profiles'),

('components');

 

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

 

CREATE TABLE IF NOT EXISTS `articles` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`title` varchar(200) NOT NULL,

`category` varchar(200) NOT NULL,

PRIMARY KEY (`id`,`title`,`category`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=93 ;

 

--

-- Dumping data for table `articles`

--

 

INSERT INTO `articles` (`id`, `title`,`category`) VALUES

(60, '63101WMH20x20','profiles'),

Link to comment
https://forums.phpfreaks.com/topic/269091-php-query-dont-work/#findComment-1382844
Share on other sites

As it stands the category table is a waste of space, it just duplicates the values held in the articles table

 

Try this, and note the quotes around profile to show it's a string value.

$sql = "SELECT * FROM articles
WHERE category = 'profile'
order by articles.id DESC
LIMIT $from, 20";

 

The correct way would be

 

articles -> id, title, cat_id

categorys -> id, category

 

Then

SELECT a.id, a.title, c.category
FROM articles a
INNER JOIN categorys c ON a.cat_id = c.id
WHERE c.category = 'profiles'
ORDER BY articles.id DESC
LIMIT $from, 20

Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/269091-php-query-dont-work/#findComment-1382851
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.