I need to insert null data if so come. I'm inserting data from EXCEL to my DB. The problem is that I made the table structure and field to allow nulls and also as a default to NULL.
The value is empty and pass all my logical validations but when inserted, left in 21:00:00
PHP :
if(!empty($r['tiempo_chip'])){
$resultados[$i]['tiempo_chip'] = $this->ExcelReader->timeFormat($r['tiempo_chip']);
}else{
$resultados[$i]['tiempo_chip'] = NULL;
}
Data :
[0] => Array
(
[numero] => 88
[tiempo_oficial] => 01:10:30
[tiempo_chip] =>
)
This is stored in the DB 21:00:00
CREATE TABLE IF NOT EXISTS `resultados` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`numero` int(11) NOT NULL,
`tiempo_oficial` time NOT NULL,
`tiempo_chip` time DEFAULT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=133 ;
Related
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'};
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?
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.
I want to merge the results of two tables in to one.
Please refer the following tables :
Data from microblog table as Row array
Array ( [ID] => 46 [userID] => 1 [userNAME] => user [blog_content] => HAI DEAR [page_name] => honda [page_ID] => 31 [post_time] => 2011-10-18 11:06:54 )
Data from Page_review table as Row array
Array ( [page_review_id] => 5 [page_id] => 31 [page_review_by_id] => 31 [page_review_by_username] => user [page_review_time] => 2011-10-18 11:43:34 [page_review_content] => hai )
Table Microblog MySQL query:
DROP TABLE IF EXISTS `featurezme_store`.`microblog`;
CREATE TABLE `featurezme_store`.`microblog` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userID` int(10) unsigned NOT NULL,
`userNAME` varchar(45) NOT NULL,
`blog_content` text NOT NULL,
`page_name` varchar(45) NOT NULL,
`page_ID` int(10) unsigned NOT NULL,
`post_time` datetime NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=latin1;
Table page_review
DROP TABLE IF EXISTS `featurezme_store`.`page_review`;
CREATE TABLE `featurezme_store`.`page_review` (
`page_review_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`page_id` int(10) unsigned NOT NULL,
`page_review_by_id` int(10) unsigned NOT NULL,
`page_review_by_username` varchar(145) NOT NULL,
`page_review_time` datetime NOT NULL,
`page_review_content` varchar(555) NOT NULL,
PRIMARY KEY (`page_review_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
the table Microblog is used to store blog posts & page_review is used to store reviews about pages[Microblog's are in pages].
My requirement is i want to show Blogposts & page reviews sorted by Date [post_time in Microblog & page_review_time on page_review].
how can i do this ? please help me.
Okay I have updated my answear, you can use union as you wanted.
Just have the same number of fields and because the results are placed the one unde the other use fields that make sense to be the one under the other. This example will bring all blogs and reviews created by a specific user (if page_review_by_id actually refers to the user id) and related to a specific page.
(
SELECT
`microblog`.`userID`,
`microblog`.`blog_content` as `blog or review`,
`microblog`.`post_time`,
`microblog`.`page_id`
from `microblog`
where `microblog`.`page_id`='1' and `microblog`.`userID`='1'
)
union
(
SELECT
`page_review`.`page_review_by_id`,
`page_review`.`page_review_content`,
`page_review`.`page_review_time`,
`page_review`.`page_id`
from `page_review`
where `page_review`.`page_id`='1' and `page_review`.`page_review_by_id`='1'
)
======== Edit== Suggesting a schema with foreign keys ================
Because I don't see any foreign keys in your schema if I could suggest optionaly a schema that applies foreign keys I present one below.
These rules are supported by this schema:
Blogs belong to the site and not to the user so there is not on delete cascade applied.
Blogs are created by users and a foreign key is applied so when a user id is inserted the consistency is assured through the foreign key.
The same goes for the page, a foreign key is applid without on delte cascade.
The same goes for the reviews table
If a user or page is deleted no child row is deleted
/********Create ***** ***/
CREATE TABLE user (
user_id int unsigned NOT NULL AUTO_INCREMENT,
username varchar(16) NOT NULL,
userpassword BLOB,
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE page (
page_id mediumint unsigned NOT NULL AUTO_INCREMENT,
title varchar(55) NOT NULL,
PRIMARY KEY (page_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE microblog (
blog_id int unsigned NOT NULL AUTO_INCREMENT,
blog_content text NOT NULL,
date_created datetime NOT NULL,
author_id int unsigned NOT NULL,
page_id mediumint unsigned NOT NULL,
PRIMARY KEY (blog_id),
CONSTRAINT blogfk1 FOREIGN KEY (author_id)
REFERENCES user (user_id),
/NO ON DELETE CASCADE/
CONSTRAINT blogfk2 FOREIGN KEY (page_id)
REFERENCES page (page_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE page_review (
review_id int unsigned NOT NULL AUTO_INCREMENT,
review_content varchar(555) NOT NULL,
date_created datetime NOT NULL,
author_id int unsigned NOT NULL,
page_id mediumint unsigned NOT NULL,
PRIMARY KEY (review_id),
CONSTRAINT reviewfk1 FOREIGN KEY (author_id)
REFERENCES user (user_id),
/NO ON DELETE CASCADE/
CONSTRAINT reviewfk2 FOREIGN KEY (page_id)
REFERENCES page (page_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/************** ******/
/** ***Insertions*** **/
INSERT INTO user ( username,userpassword)
VALUES ('username11', AES_ENCRYPT('password1',
'encription_key') ),
('username22', AES_ENCRYPT('password2',
'encription_key') );
INSERT INTO page ( title) VALUES
('title1'),('title2');
INSERT INTO microblog (blog_content,date_created,
author_id,page_id) VALUES
('blogcontent1','2011-2-2 12:00','1','1'),
('blogcontent2','2011-2-2 12:00','2','2');
INSERT INTO page_review (review_content,
date_created,author_id,page_id) VALUES
('reviewcontent1','2011-2-2 12:00','1','1'),
('reviewcontent2','2011-2-2 12:00','2','2');
/***** *******/
/******* Queries *** /
/Help on Identifing a user/
SELECT username
FROM user WHERE username ='username22'
and userpassword=AES_ENCRYPT('password2','encription_key')
(
SELECT
microblog.blog_content as blog or content,
microblog.date_created,
microblog.author_id,
microblog.page_id
from microblog
where microblog.author_id='1' and microblog.page_id='1'
)
union
(
SELECT
page_review.review_content,
page_review.date_created,
page_review.author_id,
page_review.page_id
from page_review
where page_review.author_id='1' and page_review.page_id='1'
)
I have the following table definition in MYSQL
CREATE TABLE IF NOT EXISTS `test_cases` (
`id` int(10) unsigned NOT NULL auto_increment,
`exercise_id` int(10) unsigned NOT NULL,
`author_id` int(10) unsigned NOT NULL,
`input_1_value` varchar(255) default NULL,
`input_2_value` varchar(255) default NULL,
`input_3_value` varchar(255) default NULL,
`input_4_value` varchar(255) default NULL,
`input_5_value` varchar(255) default NULL,
`output_value` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `test_cases_ibfk_1` (`exercise_id`),
KEY `author_id` (`author_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3218 ;
I have the following entry into this table
INSERT INTO `test_cases` (`id`, `exercise_id`, `author_id`, `input_1_value`, `input_2_value`, `input_3_value`, `input_4_value`, `input_5_value`, `output_value`) VALUES
(560, 145, 496, '0', NULL, NULL, NULL, NULL, '0')
I have the following query to get the above row from the table
SELECT id, exercise_id, author_id, input_1_value, input_2_value, input_3_value, input_4_value, input_5_value, output_value FROM test_cases WHERE exercise_id=145
I'm interested in, and having problems with, the value in input_1_value. Running the query in phpMyAdmin will return the value as '0', which is as expected. However running the following php script returns the value as 'true' which is completely left field and leaving me and my project supervisor stumped. The php script is below...
$db = DBCxn::getCxn();
$sql = "SELECT id, exercise_id, author_id, input_1_value, input_2_value, input_3_value, input_4_value, input_5_value, output_value FROM test_cases WHERE exercise_id=:exid";
$st=$db->prepare($sql);
$st->bindParam(":exid", $exerciseId, PDO::PARAM_INT); // $exerciseID == 145
$st->execute();
$row = $st->fetch(); // default fetch mode is PDO::FETCH_BOTH
echo $row['input_1_value'];
this echos 'true'!!!! why??? why does it not print '0'???
for even more information using print_r($row); I get the following output
Array ( [id] => 560 [0] => 560 [exercise_id] => 145 [1] => 145 [author_id] => 496 [2] => 496 [input_1_value] => true [3] => true [input_2_value] => [4] => [input_3_value] => [5] => [input_4_value] => [6] => [input_5_value] => [7] => [output_value] => true [8] => true )
Note output_value is also returned as 'true' when it should be '0'.
Does anyone know what is going on here? Any help is appreciated. GREATLY appreciated.
Try setting the constant PDO::ATTR_STRINGIFY_FETCHES to true.
Take a look at http://php.net/manual/en/pdo.setattribute.php, see if it helps.