Jump to content

Php Query Dont Work


madness69

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.