I would like to know whats the best practice in writing a database (mysql) query in codeigniter. i have seen many different ways but was confused what would be the best practice.
Following are few methods i have gone through
Method 1
$this -> db -> select('id, username, password'); $this -> db -> from('users'); $this -> db -> where('username = ' . "'" . $username . "'"); $this -> db -> where('password = ' . "'" . MD5($password) . "'"); $this -> db -> limit(1); Method 2
$query = $this->db->query("select * from user where username = '$uname' and password = '$pass'"); Method 3 (writing a stored procedure)
$query = "call authenticateuser(" . $this->db->escape($parameters['username']) . ", '" . sha1($parameters['password']) . "')"; There might may be many other ways, but would like to know what the best way in writing a query, therefore if anyone could advise, it would be really helpful.
$this -> db -> where('username',$username);. Which will automatically escape the strings for you.mysql_*functions for escaping which is in the process of deprecation. Even if you are using codeigniter, please change the default driver to pdo.