Database import situation - php

Mysql doesn't want to add this database into my localhost database section.
Am I doing something wrong?
db.sql
This tutorial: https://github.com/samanz/cakecart
Error:
SQL query:
CREATE TABLE `categories` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NULL default NULL ,
`parent_id` INT( 11 ) UNSIGNED default '0',
`order` INT( 3 ) default '0',
`image` VARCHAR( 50 ) NULL default NULL ,
`ids` VARCHAR( 225 ) NULL default NULL ,
`url` VARCHAR( 255 ) NULL default NULL ,
PRIMARY KEY ( `id` ) ,
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
UNIQUE KEY `url` ( `url` )
);
MySQL said: Documentation
#1005 - Can't create table 'cake_cart.categories' (errno: 150)

Error 150 is a foreign key problem. Likely caused by:
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
You can't make a "foreign" key reference to the same table you're creating. Simply make the parent_id column indexed instead.
KEY `parent_id` ( `parent_id` ) ,

Should look something like this...
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`parent_id` int(11) unsigned NOT NULL DEFAULT '0',
`order` int(3) NOT NULL DEFAULT '0',
`img` varchar(50) NOT NULL,
`ids` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I updated the structure and ran it on my database and it worked.

Related

I want to access the inner value of the StdClass object

Array ( [0] => stdClass Object ( [Table] => tbladmin [Create Table] => CREATE TABLE `tbladmin` ( `aid` int(11) NOT NULL AUTO_INCREMENT, `clgcode` varchar(7) NOT NULL, `name` text NOT NULL, `email` text NOT NULL, `mobile` text NOT NULL, `pass` text NOT NULL, `last_noti` int(11) NOT NULL DEFAULT 0, `profilepic` text NOT NULL, PRIMARY KEY (`aid`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 ) )
How can I access the value of the created table in laravel.
echo $array[0]->{'Create Table'};

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

Can the people of the internet help me with MySQL
Error
SQL query:
CREATE TABLE `users` (
`user_id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 32 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL ,
`first_name` VARCHAR( 32 ) NOT NULL ,
`last_name` VARCHAR( 32 ) NOT NULL ,
`email` VARCHAR( 1024 ) NOT NULL ,
`active` INT( 11 ) NOT NULL DEFAULT '0'
) ENGINE = innodb
MySQL said: Documentation
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
It clearly says in the error there can be only one auto column and it must be defined as a key.
Add PRIMARY KEY (user_id) at the end and it should work.
CREATE TABLE `users` (
`user_id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 32 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL ,
`first_name` VARCHAR( 32 ) NOT NULL ,
`last_name` VARCHAR( 32 ) NOT NULL ,
`email` VARCHAR( 1024 ) NOT NULL ,
`active` INT( 11 ) NOT NULL DEFAULT '0',
PRIMARY KEY (user_id)
) ENGINE = innodb;

MySQL: How can I list sub categories and its items, limiting them to 3?

I would like to retrieve sub categories data and its items, limiting to 3 items per subcategory
sub category 1
item1
item2
item3
sub category 2
item1
item2
item3
sub category 3
item1
item2
item3
here is my table structure
CREATE TABLE `sub_categories` (
`id` INT(11) NOT NULL,
`category_id` INT(11) NULL DEFAULT NULL,
`title` VARCHAR(255) NULL DEFAULT NULL,
`slug` VARCHAR(255) NULL DEFAULT NULL,
`active_start` DATETIME NULL DEFAULT NULL,
`active_end` DATETIME NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)]]
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
joint table
CREATE TABLE `sub_category_items` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`item_id` INT(11) NOT NULL,
`sub_category_id` INT(11) NOT NULL,
`created` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=64;
CREATE TABLE `items` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NULL DEFAULT NULL,
`created` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=64;
here is my MySQL query
SELECT `Item`.`title`,`SubCategoryItem`.`id`, `SubCategoryItem`.`item_id`, `SubCategoryItem`.`sub_category_id`, `SubCategoryItem`.`created`, `SubCategory`.`id`, `SubCategory`.`category_id`, `SubCategory`.`title`, `SubCategory`.`active_start`, `SubCategory`.`active_end`, `SubCategory`.`created`
FROM `items` AS `Item`
left JOIN `sub_category_item` AS `SubCategoryItem` ON (`SubCategoryItem`.`item_id`=`Item`.`id`) right JOIN `sub_categories` AS `SubCategory` ON (`SubCategoryItem`.`sub_category_id`=`SubCategory`.`id`)
WHERE `SubCategory`.`category_id` = 21 ORDER BY CASE
WHEN MONTH(`SubCategory`.`active_start`) = MONTH(CURDATE()) THEN 0
WHEN MONTH(`SubCategory`.`active_end`) >= MONTH(CURDATE()) THEN 1
WHEN MONTH(`SubCategory`.`active_start`) <= MONTH(CURDATE()) THEN 2
ELSE 3
END , MONTH(`SubCategory`.`active_start`) desc
what am I doing wrong ?

Model HABTM Model Referencing Original Model

I have a Team model that is HABTM Match and when I pull the data for a specific Team, I ask for the related Teams for those Matches, but Cake only returns the data for the original Team.
How can I get all the Teams (the opponent) for that Match without looping through the results and pulling them that way?
I am having the same issue with the Team HABTM Player association as well. When I pull a Player, Cake will not return any of the associated Players (teammates) for the linked Team.
My schema:
CREATE TABLE IF NOT EXISTS `matches` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tournament_id` int(10) unsigned NOT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
`created` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `tournament_id` (`tournament_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `matches_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`match_id` int(10) unsigned NOT NULL,
`team_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `match_id` (`match_id`,`team_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `players` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `players_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`player_id` int(10) unsigned NOT NULL,
`team_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `player_id` (`player_id`,`team_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tournament_id` int(10) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`seed` smallint(2) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `tournament_id` (`tournament_id`),
KEY `seed` (`seed`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
My query:
$this->Team->recursive = 3;
$team = $this->Team->read(null, $id);
$this->set('team', $team);
I have also tried:
$this->Team->contain(array(
'Match' => array(
'Team',
),
));
$team = $this->Team->read(null, $id);
$this->set('team', $team);
The results ($team):
array (size=4)
'Team' =>
array (size=5)
... team data ...
'Match' =>
array (size=2)
0 =>
array (size=9)
... match data ...
'MatchesTeam' =>
array (size=3)
'id' => string '1' (length=1)
'match_id' => string '1' (length=1)
'team_id' => string '1' (length=1)
// there should be an array for 'Team' here
// that contains the opponent team
1 =>
... more match data with same missing 'Team' array ...
... other related models ...

How do I import this database correctly?

How do I import this database correctly?
https://github.com/samanz/cakecart
Every time I import then I get this error:
Error
SQL query:
CREATE TABLE `categories` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NULL default NULL ,
`parent_id` INT( 11 ) UNSIGNED default '0',
`order` INT( 3 ) default '0',
`image` VARCHAR( 50 ) NULL default NULL ,
`ids` VARCHAR( 225 ) NULL default NULL ,
`url` VARCHAR( 255 ) NULL default NULL ,
PRIMARY KEY ( `id` ) ,
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
UNIQUE KEY `url` ( `url` )
);
MySQL said: Documentation
#1005 - Can't create table 'db.categories' (errno: 150)
Foreign key is error 150, but there's much more tables than this error.
Please try import first then answer.
THis one works:
CREATE TABLE `categories` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NULL default NULL ,
`parent_id` INT( 11 ),
`order` INT( 3 ) default '0',
`image` VARCHAR( 50 ) NULL default NULL ,
`ids` VARCHAR( 225 ) NULL default NULL ,
`url` VARCHAR( 255 ) NULL default NULL ,
PRIMARY KEY ( `id` ) ,
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
UNIQUE KEY `url` ( `url` )
);
Edit: Actually you only need to remove unsigned to make it work. But I don't really know why you want it to be default 0. It should be default NULL which is the default default.. :)
I believe the type of parent_id must be the same as id.
The problem is that,You are trying to create table category.In this table you are using forign key but this forign key included table isnot created yet.
So first create the table contaning parent_id .After that try to create the category table
I'm think it's giving you and error because you're referencing an nonexistent, not yet created, table which is the table your trying to create itself.
why don't you create the table first then add the constraint(foreign key)?
but there's a possible error that you may run into whole doing this in the same table. what would happen if the newly added record doesn't have a parent assigned to it? default value is 0. will this give you an error because there's no record with id=0?
I suggest you normalize this by creating a relationship table for this.
CREATE TABLE CategoryGroups //or whatever name you find fits.
(
`Cat_id` int(11) NOT NULL,
`Parent_id` int(11),
FOREIGN KEY (`Parent_id`) REFERENCES categories(`id`),
FOREIGN KEY (`Cat_id`) REFERENCES categories(`id`)
)
Best practices would be to normalize all data and remove all many to many relationships between any two tables by creating relationship tables.

Categories