本文整理汇总了PHP中DatabaseConnection::lastInsertId方法的典型用法代码示例。如果您正苦于以下问题:PHP DatabaseConnection::lastInsertId方法的具体用法?PHP DatabaseConnection::lastInsertId怎么用?PHP DatabaseConnection::lastInsertId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DatabaseConnection
的用法示例。
在下文中一共展示了DatabaseConnection::lastInsertId方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: insert
/**
* Inserts an artist in database.
*
* @return bool True on success or false on failure
*/
public function insert()
{
require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php';
$connection = new DatabaseConnection();
$query = $connection->prepare('INSERT INTO `artist` (`name`, `mbid`, `summary`, `country`) VALUES (:name, :mbid, :summary, :country);');
$query->bindValue(':name', $this->name, PDO::PARAM_STR);
$query->bindValue(':mbid', $this->mbid, PDO::PARAM_STR);
$query->bindValue(':summary', $this->summary, PDO::PARAM_STR);
$query->bindValue(':country', $this->country, PDO::PARAM_STR);
if ($query->execute()) {
$this->id = $connection->lastInsertId();
//returns insertion was successfully processed
return true;
}
//returns insertion has encountered an error
return false;
}
示例2: insert
/**
* Inserts an album in database.
*
* @return bool True on success or false on failure
*/
private function insert()
{
require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php';
$connection = new DatabaseConnection();
$query = $connection->prepare('INSERT INTO `album` (`name`, `mbid`, `artist`, `year`, `disk`, `country`, `mbidGroup`) VALUES ( :name, :mbid, :artist, :year, :disk, :country, :mbidGroup);');
$query->bindValue(':name', $this->name, PDO::PARAM_STR);
$query->bindValue(':mbid', $this->mbid, PDO::PARAM_STR);
$query->bindValue(':artist', $this->artist, PDO::PARAM_INT);
$query->bindValue(':year', $this->year, PDO::PARAM_INT);
$query->bindValue(':disk', $this->disk, PDO::PARAM_INT);
$query->bindValue(':country', $this->country, PDO::PARAM_STR);
$query->bindValue(':mbidGroup', $this->mbidGroup, PDO::PARAM_STR);
if ($query->execute()) {
$this->id = $connection->lastInsertId();
//returns insertion was successfully processed
return true;
}
//returns insertion has encountered an error
return false;
}
示例3: insert
/**
* Inserts a track in database.
*
* @return bool True on success or false on failure
*/
public function insert()
{
require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php';
$connection = new DatabaseConnection();
$query = $connection->prepare('INSERT INTO `track` (`file`, `album`, `year`, `artist`, `title`, `bitrate`, `rate`, `mode`, `size`, `time`, `track`, `mbid`, `updateTime`, `additionTime`, `composer`) VALUES (:file, :album, :year, :artist, :title, :bitrate, :rate, :mode, :size, :time, :track, :mbid, :update, :addition, :composer);');
$query->bindValue(':file', $this->file, PDO::PARAM_STR);
$query->bindValue(':album', $this->album, PDO::PARAM_INT);
$query->bindValue(':year', $this->year, PDO::PARAM_INT);
$query->bindValue(':artist', $this->artist, PDO::PARAM_INT);
$query->bindValue(':title', $this->title, PDO::PARAM_STR);
$query->bindValue(':bitrate', $this->bitrate, PDO::PARAM_INT);
$query->bindValue(':rate', $this->rate, PDO::PARAM_INT);
$query->bindValue(':mode', $this->mode, PDO::PARAM_STR);
$query->bindValue(':size', $this->size, PDO::PARAM_INT);
$query->bindValue(':time', $this->time, PDO::PARAM_INT);
$query->bindValue(':track', $this->track, PDO::PARAM_INT);
$query->bindValue(':mbid', $this->mbid, PDO::PARAM_STR);
$query->bindValue(':update', time(), PDO::PARAM_INT);
$query->bindValue(':addition', time(), PDO::PARAM_INT);
$query->bindValue(':composer', $this->composer, PDO::PARAM_STR);
if ($query->execute()) {
$this->id = $connection->lastInsertId();
//return true to indicate a successful track insertion
return true;
}
//return false to indicate an error during track insertion
return false;
}
示例4: create
/**
* Create user with provided informations.
*
* @param object $user User with his values attributes
* @param string $error The returned error message
*
* @return bool True if the user is created
*/
public function create($user, &$error)
{
$error = '';
require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/DatabaseConnection.php';
$connection = new DatabaseConnection();
$query = $connection->prepare('INSERT INTO `user` (`login`, `name`, `email`, `password`, `status`) VALUES (:login, :name, :email, :password, :status);');
$query->bindValue(':login', $this->login, PDO::PARAM_STR);
$query->bindValue(':name', $this->name, PDO::PARAM_STR);
$query->bindValue(':email', $this->email, PDO::PARAM_STR);
$query->bindValue(':password', md5($this->password), PDO::PARAM_STR);
$query->bindValue(':status', $this->status, PDO::PARAM_INT);
if ($query->execute() && $query->rowCount() > 0) {
$this->id = $connection->lastInsertId();
//return true to indicate a successful user creation
return true;
}
$error = $query->errorInfo()[2];
//try to return intelligible error
if ($query->errorInfo()[1] === 1062 || $query->errorInfo()[2] === 'UNIQUE constraint failed: user.login') {
$error = 'login `' . $this->login . '` already exists';
}
//return false to indicate an error occurred while creating user
return false;
}