Reputation: 334
Here is the functions I've created so far
class MySQLiDatabase{
/**
* Database credentials
*/
private $db_host = ''; // localhost
private $db_name = ''; // database name
private $db_user = ''; // database uer
private $db_pass = ''; // database password
/**
* create conneaction
* @string
*/
private $connection;
/**
* start connection as soon as this file gets load
*/
public function __construct(){
$this->connect();
}
/**
* connect to the database
*/
public function connect(){
$this->connection = mysqli_connect($this->db_host,$this->db_user,$this->db_pass);
if(!$this->connection){
die ("Database connection failed: " . mysql_error());
}else{
$select_db = mysqli_select_db($this->connection,$this->db_name);
if(!$select_db){
die("Database selection failed: " . mysql_error());
}
}
}
/**
* close mysql connection
*/
public function close_connection(){
if(isset($this->connection)){
mysqli_close($this->connection);
unset($this->connection);
}
}
/**
* run database query
*/
public function query($sql){
$result = mysqli_query($this->connection,$sql);
$this->confirm_query($result);
return $result;
} // end query($sql)
/**
* confirm mysql query
* @param string
*/
public function confirm_query($result){
if(!$result){
die ("Database query failed: " . mysqli_errno());
}
}
/**
* Insert into database
* @param string
* return TRUE
*/
public function insert($table, $keys, $values){
$condition = "INSERT INTO $table ($keys) VALUES('".$values."')";
$query = $this->query($condition);
return $query;
}
}
$database = new MySQLiDatabase();
$db =& $database;
Now I would like to create insert() function which is working fine but when I am inserting more data in it its giving me error which I have defined in confirm_query() functions
$database->insert('table', 'username', 'test');
I want to acomplish something like below
$database->insert('table', 'username,password,email', 'test,test,[email protected]');
Upvotes: 0
Views: 3044
Reputation: 334
Finally the problem got solved Thanks @VIDesignz for helping me so far with "quots"
Here is the function
public function insert($table, $column, $value){
$condition = "INSERT INTO $table ($column) VALUES ($value)";
$query = $this->query($condition);
return $query;
}
and this is how I am running my query
$database->insert( "table" , 'username,password,email' , " 'shaz3e' , 'securepassword', '[email protected]' ");
next is to create Update/Delete functions
Upvotes: 1
Reputation: 4783
Ok, I can't say your approach is the best but if you are stuck on doing it this way. At least use prepared statements....
(Not tested but should work fine)
Set up the function like this
public function insert($table, $columns, $values, $execute){
$sql = "INSERT INTO
$table ($columns)
VALUES ($values)";
$query = $database->prepare($sql);
$query->execute($execute);
}
Then with each call you have to set the values like this
$table = 'table_name';
$columns = 'column_1, column_2, column_3';
$values = ':value_1, :value_2, :value_3';
$execute = array(':value_1' => 'First Value',
':value_2' => 'Second Value',
':value_3' => 'Third Value');
then run the function like this
$database->insert($table, $columns, $values, $execute);
Upvotes: 0