PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] how to check, if authenticated user in array from db

 December 30, 2021     yii     No comments   

Issue

In my DB I have a project table which has

  project
  id | title | users |
  1  | First | 1,2,3 |
  2  | Second| 2,6   |

how can I check Yii::app()->user->id contained in users field? I want to check, if true then show row in gridview.

Which $criteria must I use?


Solution

Use LIKE query for checking your user id exist or not in users column.

$user = Yii::app()->user->id;
$result = Yii::app()->db->createCommand('SELECT * FROM project WHERE users  LIKE "%'.$user.'%"')->queryAll();
if(count($result)){
    //Your other scripts to show in grid view
}

If you have a Project model class then you also can use:

$user = Yii::app()->user->id;
$criteria = new CDbCriteria;
$criteria->select = '*';
$criteria->compare('t.users', $user, true);
$results = Project::model()->findAll($criteria);


Answered By - alamincse07
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing