-
-
Save arunoda/69bb6b1d48831b9025da to your computer and use it in GitHub Desktop.
Codeigniter - Interact with a Database using SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User extends CI_Controller { | |
public function __construct() { | |
parent::__construct(); | |
$this->load->database(); | |
} | |
public function insert($username, $password) { | |
$sql = "INSERT INTO users (username, password) VALUES(?, ?)"; | |
$this->db->query($sql, array($username, $password)); | |
echo "Inserted"; | |
} | |
public function select() { | |
$sql = 'SELECT * FROM users'; | |
$result = $this->db->query($sql)->result_array(); | |
foreach($result as $user) { | |
echo "{$user['username']} - {$user['password']}<br>"; | |
} | |
} | |
public function update($username, $password) { | |
$sql = "UPDATE users SET password=? WHERE username=?"; | |
$this->db->query($sql, array($password, $username)); | |
echo "Updated"; | |
} | |
public function delete($username) { | |
$sql = "DELETE FROM users WHERE username = ?"; | |
$this->db->query($sql, array($username)); | |
echo "Deleted"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment