这是我到目前为止创建的函数
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;
现在我想创建一个工作正常的insert()函数,但是当我在其中插入更多数据时,它给出了我在confirm_query()函数中定义的错误
$database->insert('table', 'username', 'test');
我想要完成下面的内容
$database->insert('table', 'username,password,email', 'test,test,test@email.com');
答案 0 :(得分:1)
最后问题解决了感谢@VIDesignz帮助我到目前为止用#34; quots"
这是函数
public function insert($table, $column, $value){
$condition = "INSERT INTO $table ($column) VALUES ($value)";
$query = $this->query($condition);
return $query;
}
这就是我运行查询的方式
$database->insert( "table" , 'username,password,email' , " 'shaz3e' , 'securepassword', 'email@email.com' ");
接下来是创建更新/删除功能
答案 1 :(得分:0)
好的,我不能说你的方法是最好的,但如果你坚持这样做的话。至少使用准备好的陈述......
(未经测试但应该可以正常工作)
设置这样的功能
public function insert($table, $columns, $values, $execute){
$sql = "INSERT INTO
$table ($columns)
VALUES ($values)";
$query = $database->prepare($sql);
$query->execute($execute);
}
然后每次调用你必须设置像这样的值
$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');
然后运行像这样的函数
$database->insert($table, $columns, $values, $execute);