Skip to content

Instantly share code, notes, and snippets.

@DavidMellul
Last active March 14, 2018 23:06
Show Gist options
  • Select an option

  • Save DavidMellul/9df4126700377a1c917db849e12c7ddb to your computer and use it in GitHub Desktop.

Select an option

Save DavidMellul/9df4126700377a1c917db849e12c7ddb to your computer and use it in GitHub Desktop.
<?php
// CodeIgniter coding style with plain queries, easy to read
$users = $this->db->query('SELECT * FROM Users')->result();
$cities = $this->db->query('SELECT * FROM Cities')->result();
$usersWithCity = $this->db->query('SELECT U.*, C.city_name FROM Users U LEFT JOIN Cities C ON U.id_user = C.id_user')->result();
$messages = $this->db->query('SELECT * FROM Messages ORDER BY date_sent');
/*
* Using LEFT JOIN in server-side web development is a good practice (IMHO) and here's why :
*
* Consider tables A and B
*
* Using LEFT JOIN will KEEP A's records even if they don't have any reference to B ones
* Using INNER JOIN will SKIP A's records not having a reference to B ones
*
* However, you'd like to be able to show A's data with a special clause like :
* show_A(); if ( there_is_nothing_from_B() ) show_empty_message();
*
* A common scenario is instant-messaging apps, you want the user profile and its messages
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment