PDO Select Join Operation

Description

Select records from the multiple table with join keyword. You can provide columns to be selected and where clause with associative array of conditions with keys as columns and value as column value, group by, order by , limit etc. You needs to specify join condition between different tables and join type (left join, right join etc.) to select data. This function will automatically creates query for you and select data.

Function Reference :
 
          	array dbSelectJoin(string $table_names, array $join_conditions, array $join_type [, array $columns=array()] [, assoc. array $select_where_condition=array()]) 
                   

Parameters

Name Type Required Parameter Info Example
table_name array Yes Array of tables to be joined. any table name like array("employee_table","user_table")
join_conditions array Yes Array of join conditions between tables array("users.role_id=user_roles.role_id")
join_type array Yes Array of join types array("INNER JOIN","LEFT JOIN")
columns array Optional Array of columns to be selected array("firstname","lastname","role_name")
select_where_condition associative array Optional Associative array with key as column name and values as column value for where clause array("gender"=>"male")

Examples

  
                 <?php             
                 $sdb= new SimplifiedDB();
                 $sdb->dbConnect("localhost","username","password","dbuser"); 
                 $tables=array("users", "user_roles","user_blogs");
                 $joins=array("users.role_id=user_roles.roleid","users.userid=user_blogs.userid");
                 $join_condition=array("LEFT JOIN","RIGHT JOIN");
                 $sdb->dbSelectJoin($tables,$joins,$join_condition); 
                 ?> 

Result

Returns array of values. You can define which type of array you needs using $sdb->fetch_mode="ASSOC" or $sdb->fetch_mode="NUM" etc. By default, it retrives associative array.

If you use print_r($result_array), it will show output as in following format.

Array ( [0] => Array ( [firstName] => Ram [email] => y.ram11@gmail.com [role_name] => Contributer [blogTitle] => first [content] => first ) [1] => Array ( [firstName] => da [email] => y.ram11@gmail.com [role_name] => Contributer [blogTitle] => second [content] => second ) )

Debuging

Debug Operation Result/Output
$sdb->query SELECT * FROM `users` LEFT JOIN `user_roles` ON users.role_id=user_roles.roleid RIGHT JOIN `user_blogs` ON users.userid=user_blogs.userid
$sdb->last_insert_id
$sdb->rows_affected
$sdb->message_info Connected to database
$sdb->error_info

More Examples

 
             <?php       
             $tables=array("users", "user_roles");
             $joins=array("users.role_id=user_roles.roleid");
             $join_condition=array("LEFT JOIN");
             $columns=array("firstName", "email", "role_name","blogTitle","content");
             $whereCondition=array("gender"=>"male");
             $sdb->dbSelectJoin($tables,$joins,$join_condition,$columns,$whereCondition);
             ?>