Can' fetch row role from database using Nette - php

I got my table like this:
`id` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(50) NOT NULL,
`role` varchar(10),
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`email`)
and I have data like this
1 | admin | 702a7853691c23ec922e408b1322d2cc4fa1d101 | name#doman.sk | guest
I try to SELECT data:
$row = $this->database->table('users')->where('username', $username)->fetch();
I can access:
$row->email,$row->username,$row->password
but i can't access :
$row->role
I can't find sollution for this.

I don't know you are using which mvc or cms. But i know that after each select you must fetch your data. See your documents for fetch syntax.

Related

MySQL InnoDB AUTO_INCREMENT secondary column

I am making a system where users can upload any file they want, and not use it to execute any kind of code. As a part of that, I rename every file, and store its original name in a MySQL table. This table contains the id of the user who uploaded it, and a unique id of the upload. Currently I am doing it like this:
CREATE TABLE `uploads` (
`user_id` INT(11) NOT NULL,
`upload_id` INT(11) NOT NULL AUTO_INCREMENT,
`original_name` VARCHAR(30) NOT NULL,
`mime_type` VARCHAR(30) NOT NULL,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`user_id`, `upload_id`)
) ENGINE=MyISAM;
This means I will always have a unique combination of user_id and upload_id, and every users first upload has an id of 1. However I want to use a foreign key for the user_Id, so if I delete a user, its uploads would also be deleted. This means I have to do it in InnoDB. How would i go about that, since the above setup only works in MyISAM.
My users table (wich i would get user_id from) looks like this:
CREATE TABLE `".DATABASE."`.`users` (
`user_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(30) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`password` CHAR(128) NOT NULL,
`salt` CHAR(128) NOT NULL
) ENGINE = InnoDB;
What i want is for the uploads table to look like this:
user_id | upload_id
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
1 | 3
If that makes sense
If I understood correctly:
Replace the primary key to a unique index with the two fields. Make the upload_id the primary key and user_id the foreign key then.
Unfortunately for this problem, the auto increment column will not start over at 1 for each user. It will just increment for each added row, regardless of any particular column value.
Edit: displaying my ignorance there, MyISAM tables apparently will start over at 1 for each user when a multi-column index this way. https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
Fortunately, it is probably not necessary for upload_id to start at 1 for each user. Using auto increment on that column means you will always have a unique combination of user_id and upload_id even without using both columns as the primary key, or even creating a unique index, because every record will have a different upload_id. You should still be able to implement the cascade delete with this setup.

MySQL: Fastest way to sum fields in table within Tree behavior in CakePHP

I have table users with tree behavior
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`lft` int(11) DEFAULT NULL,
`rght` int(11) DEFAULT NULL,
`user_email` varchar(255) NOT NULL DEFAULT '',
`user_password` char(100) NOT NULL DEFAULT '',
`user_name` varchar(255) NOT NULL DEFAULT ''
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and table trades
CREATE TABLE `trades` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`requests` float DEFAULT NULL,
`trade_date_start` datetime DEFAULT NULL,
`trade_date_stop` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
and now I need to count sum of all 'requests' (each user can have more than hundreds of 'trades' records, new record is generated once per 4 hours) per user within my tree so I will get (sum of requests in parenthesis) and this is count
in scope of one month.
me-
|
|_ John (20)
| |
| |_ John2 (200)
| |_ Jane (233)
| |_ George (3233)
|
|_ Alena (500)
...
So each month I need to see sum of request for each user from 1st day of the current month til last day of current month.
It has to be as fast as possible. COuld somebody help me? Thank you
This problem begs for a solution via recursive query, which MySQL alone among commonly used DBMSs does not support. As a result, I think you'll need to perform the necessary recursion yourself if you're tied to MySQL. Each query might have this form:
SELECT
user_id,
SUM(requests) AS requests,
FROM
users
JOIN trades
ON users.id = trades.user_id
WHERE
users.parent_id = <PARENT_ID>
AND trade_date_start BETWEEN <WINDOW_START_TIMESTAMP> AND <WINDOW_END_TIMESTAMP>
GROUP BY user_id
You would need to process each result row and recursively issue the same sort of query for each user_id returned.
Since you'll be issuing possibly many queries with the same date filtering condition, you might speed it up by first creating a temporary table containing just those rows from trades that fall in the window of interest, then using that (with no explicit date condition) instead of table trades.

Different user roles for logging in with PHP and Mysql

I have a project coming up for doing Admin functions so my question is this. I will try and be clear as possible.
I will have one SUPER-USER who updates all information for other regular-users/people(being our clients).
The clients/regular-users when they log in will only see their info and download files uploaded by SUPER-USER and not see for regular-users.
So if you are Client:#01 you will see the dashboard (welcome page) and your info. Can anyone suggest possible database designs for this.
How to use left/right sql-joins between the user and files table?
UPDATE
I have a users table as well as a company table that the user belongs to. So essentially I want something like this::
$sql = select everything in the users table where the username and pass = to the given form, then left or right join that username to the company that he belong to.
Then they will see their information. if logged in successfully. Because user #01 belongs to company #03 /#01 etc...
USER TABLE looks so
`id` int(11) NOT NULL AUTO_INCREMENT,
`fname` varchar(50) NOT NULL,
'lname` varchar(100) NOT NULL,
`username` varchar(50) ,
`password` varchar(100) ,
`company` varchar(50) // the company name that ther user belongs to
PRIMARY KEY (`id`)
COMPANY table
'id' int(11) not null auto_increment,
'user_id' int(11) //This is to tie the users to this table
'description' varchar(text),
'filename' varchar(25) not null,
'mimetype' varchar (25) not null
PRIMARY KEY ('id')
Well, it depends on how simple or complex you want to go. with something like this I usually will keep it relatively simple and have a main user database (for all users) example:
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(255) NOT NULL AUTO_INCREMENT,
`user_name` varchar(50) NOT NULL,
`user_pass` varchar(100) NOT NULL,
`user_permissions` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(1) NOT NULL DEFAULT '1',
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MYISAM;
Then I would have possible a second table of permissions depending on how many permissions I was going to have. If all you are going to have is users and super users then you could probably just assign users a value of 0 and then super user a value of 1.
Then in your PHP script it would treat the users different based on their "user_permissions" value.
Now if you are intending to have lots of different levels of permissions then I would definitely create at least one more table to define permissions example:
CREATE TABLE IF NOT EXISTS `permission` (
`permission_id` int(255) NOT NULL AUTO_INCREMENT,
`permission_name` varchar(100) NOT NULL,
`permission_value` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MYISAM;
Then in the permissions table you could assign all sorts of different permissions... read, write, publish, admin, regular user, super user etc.
This is just a very simple starting point. hope that helps.

problem with mysql query

i have a table called users
this what the table look like
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(60) NOT NULL,
`email` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
)
and finally i have a table called friends,
this what the table look like
CREATE TABLE `friends` (
`friendship_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id1` bigint(20) unsigned NOT NULL,
`user_id2` bigint(20) unsigned NOT NULL,
`time_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`friendship_id`),
KEY `user_id1` (`user_id1`),
KEY `user_id2` (`user_id2`),
CONSTRAINT `friends_ibfk_1` FOREIGN KEY (`user_id1`) REFERENCES `users` (`id`),
CONSTRAINT `friends_ibfk_2` FOREIGN KEY (`user_id2`) REFERENCES `users` (`id`)
)
so basically if userA is following userB , then a row will be added to the friends table, with the attribute user_id1 is userA and user_id2 is userB.
im trying to write a mysql query for a searchbox. the user will enter a string and the query will crawl a list of users that include that string but the people that the user is following need to be displayed first.
so if we have 3 users
jack
jason
john
if the user Chris (who's following jason) enters in the searchbox the string 'ja', the query will crawl the list of users with the following order jason,jack. since jason is followed by chris.
from my understanding , i think it might a group by problem, i tried different queries but i couldnt get the needed results
do you guys have any idea ?
thanks a lot
You have to do a trick for sorting, so friendships get a 0 and non-friendships get a 1 in a temporary field and then we sort ascending for this field and as second we sort by username
SELECT x.username
FROM users x LEFT JOIN friends y ON x.id=y.user_id2 AND y.user_id1=$LOGGED_IN_USER
WHERE LOWER(x.username) LIKE 'ja%'
ORDER BY CASE WHEN y.user_id2 IS NULL THEN 1 ELSE 0 END,x.username
#thanks to scwagner for pointing me to extend JOIN-clause

MySQL table column description help needed

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`status` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
)
Please explain me what is doing UNIQUE KEY 'username' (username') statement in the example above and why ('username') is written once again?
The UNIQUE_KEY line is creating a unique index called 'username' on the column 'username'. A unique index allows only a single record to have a specific value. This is useful on rows like usernames because it prevents two users from being created with the same username.
However, I think you would get an error if you ran this because you have not defined a column called username.
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`status` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
)
This is simply the format MySQL's CREATE TABLE syntax expects. If you look at the syntax in detail, you'll see...
UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
In other words, you're using an index_name of "username" which uses the "username" field/column. This might seem odd, but if you were using a compound key, you'd might have a definition something like...
UNIQUE KEY duplicate_lock (user_email, user_name)
...so you'd have different index name and column portions of the definition.

Categories