我是php包装类的新手...这里我包含了类文件...我不知道如何编写插入查询... Plz告诉我如何使用下面的编写插入查询类...
class Database {
public $connection;
private $host = "localhost";
private $username = "root";
private $password = "";
private $dbname = "property";
public function __construct() {
$this->connection = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function insert($query, array $data) {
$this->connection->prepare($query)->execute($data);
return $this->connection->lastInsertId();
}
public function update($query, array $data) {
$stmt = $this->executeQuery($query, $data);
return $stmt->rowCount();
}
public function delete($query, array $data) {
$stmt = $this->executeQuery($query, $data);
return $stmt->rowCount();
}
public function findOne($query, array $data = null) {
$stmt = $this->executeQuery($query, $data);
return $stmt->fetchObject();
}
public function findMany($query, array $data = null) {
$stmt = $this->executeQuery($query, $data);
return($stmt->fetchAll(PDO::FETCH_OBJ));
}
public function executeQuery($query, $data = null) {
$stmt = $this->connection->prepare($query);
$stmt->execute($data);
return $stmt;
}
}
答案 0 :(得分:0)
您已经拥有inset()
方法,因此请使用
$db = new Database();
$lastId = $db->insert(
'INSERT INTO `myTable` (`name`, `title`) VALUES (:name, :title)',
[
':name' => 'Name of entry',
':title' => 'Title of entry',
],
);
var_dump($lastId);