I want to access the inner value of the StdClass object - php

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'};

Related

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 ?

mysql join query not returning correct values

Currently I have a loop that retrieves data from 2 tables, users and clients, for each row. So the queries are stacking up.
Firstly, is this a bad thing?
I'm looking to display 20 rows per page, so currently that work be about 45 queries.
Here's a list of the queries when I'm displaying only 2 rows:
Database queries
#1
SELECT CID FROM users WHERE Hash = :Hash LIMIT 1;
#2
SELECT start_time,finish_time,dinner_time FROM company WHERE CID = :CID LIMIT 1;
#3
SELECT CID,Access,Hash FROM users WHERE CID = :CID LIMIT 1;
#4
SELECT count(*) FROM jobs WHERE CID = :CID AND ( ( Status="1" ) OR ( Status="2" ) ) AND SUBSTRING(LOWER(`SiteName`), 1, 1) REGEXP '[[:digit:]]';
#5
SELECT * FROM jobs WHERE CID = :CID AND ( ( Status="1" ) OR ( Status="2" ) ) AND SUBSTRING(LOWER(`SiteName`), 1, 1) REGEXP '[[:digit:]]' ORDER BY JID DESC LIMIT 20;
#6
SELECT UID,FirstName,LastName FROM users WHERE CID = :CID AND UID = :UID LIMIT 1;
#7
SELECT ClientID,Name FROM clients WHERE CID = :CID AND ClientID = :ClientID LIMIT 1;
#8
SELECT UID,FirstName,LastName FROM users WHERE CID = :CID AND UID = :UID LIMIT 1;
#9
SELECT ClientID,Name FROM clients WHERE CID = :CID AND ClientID = :ClientID LIMIT 1;
#10
SELECT UID,FirstName,LastName FROM users WHERE CID = :CID AND UID = :UID LIMIT 1;
What I'm thinking is joining the users and clients into one query (6-7 from the list above)
SELECT u.UID, u.FirstName, u.LastName, c.ClientID, c.Name FROM users u INNER JOIN clients c WHERE u.CID = :CID AND c.CID = :CID2 AND u.UID = :UID AND c.ClientID = :ClientID
This is what is returned:
Array
(
[0] => Array
(
[name] => UID
[value] => 1
[type] => 1
)
[1] => Array
(
[name] => ClientID
[value] => 8
[type] => 1
)
[2] => Array
(
[name] => CID
[value] => 1
[type] => 1
)
[3] => Array
(
[name] => CID2
[value] => 1
[type] => 1
)
)
I was expecting is to return like:
Array
(
[UID] => 1
[FirstName] => John
[LastName] => Smith
[ClientID] => 1
[Name] => Client Name
)
Anyone know where I'm going wrong?
Update
clients table
CREATE TABLE IF NOT EXISTS `clients` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`CID` int(11) NOT NULL,
`ClientID` int(11) DEFAULT NULL,
`Name` varchar(100) NOT NULL,
`Clientcontact` varchar(100) NOT NULL,
`Prefix` varchar(20) NOT NULL,
`Email` varchar(50) NOT NULL,
`Phone` varchar(12) NOT NULL,
`Fax` varchar(12) NOT NULL,
`Address1` varchar(100) NOT NULL,
`Address2` varchar(100) NOT NULL,
`Address3` varchar(100) NOT NULL,
`County` varchar(100) NOT NULL,
`Post` varchar(100) NOT NULL,
`Invoicecontact` varchar(100) NOT NULL,
`Invoiceemail` varchar(50) NOT NULL,
`Invoiceaddress1` varchar(100) NOT NULL,
`Invoiceaddress2` varchar(100) NOT NULL,
`Invoiceaddress3` varchar(100) NOT NULL,
`Invoicecounty` varchar(100) NOT NULL,
`Invoicepost` varchar(100) NOT NULL,
`Dateadded` int(10) NOT NULL,
`vat` tinyint(1) DEFAULT NULL,
`vatnumber` int(14) DEFAULT NULL,
`Status` tinyint(1) NOT NULL,
PRIMARY KEY (`ID`)
)
users table
CREATE TABLE IF NOT EXISTS `users` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`CID` int(11) DEFAULT NULL,
`UID` int(11) DEFAULT NULL,
`FirstName` varchar(25) DEFAULT NULL,
`LastName` varchar(25) DEFAULT NULL,
`dob1` varchar(2) DEFAULT NULL,
`dob2` varchar(2) DEFAULT NULL,
`dob3` varchar(4) DEFAULT NULL,
`Email` varchar(50) DEFAULT NULL,
`password_hash` text NOT NULL,
`api_key` varchar(32) NOT NULL,
`api_sync_key` varchar(10) NOT NULL,
`api_sync_word` varchar(10) NOT NULL,
`Hash` varchar(32) DEFAULT NULL,
`start_time` decimal(4,2) DEFAULT NULL,
`end_time` decimal(4,2) DEFAULT NULL,
`dinner_time` decimal(4,2) DEFAULT NULL,
`Phone_A` varchar(15) DEFAULT NULL,
`Phone` varchar(15) DEFAULT NULL,
`MobileNum` varchar(15) DEFAULT NULL,
`WorkNum` varchar(15) DEFAULT NULL,
`Emg` varchar(50) DEFAULT NULL,
`EmgNum` varchar(15) DEFAULT NULL,
`Address1` varchar(100) DEFAULT NULL,
`Address2` varchar(100) DEFAULT NULL,
`Address3` varchar(100) DEFAULT NULL,
`County` varchar(100) DEFAULT NULL,
`Post` varchar(100) DEFAULT NULL,
`DateJoined` varchar(30) DEFAULT NULL,
`LastLogged` int(11) DEFAULT NULL,
`DateLeft` int(11) DEFAULT NULL,
`Contract` int(3) DEFAULT NULL,
`Pay` int(3) DEFAULT NULL,
`Position` int(3) DEFAULT NULL,
`Active` varchar(255) DEFAULT NULL,
`Access` tinyint(2) DEFAULT NULL,
PRIMARY KEY (`ID`)
)
If your table structure is as follow then you should write your query as I have written.
TABLE NAME: Users ( Your Primary Table )
UID PK
FirstName
LastName
TABLE NAME: Clients
ClientID PK
NAME
UID FK
SELECT u.UID AS UID,
u.FirstName AS FirstName,
u.LastName AS LastName,
c.ClientID AS ClientID,
c.Name AS NAME
FROM users AS u
LEFT JOIN clients AS c ON u.UID = c.UID
WHERE u.UID = :UID;

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

Database import situation

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.

MYSQL PDO return type strange conversion

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.

Categories