本文整理汇总了PHP中PDO::lastInsertID方法的典型用法代码示例。如果您正苦于以下问题:PHP PDO::lastInsertID方法的具体用法?PHP PDO::lastInsertID怎么用?PHP PDO::lastInsertID使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PDO
的用法示例。
在下文中一共展示了PDO::lastInsertID方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: Query
/** @return PDOStatement|bool */
public function Query($string, $returnID = false, $prettify = MYSQLLOGPRETTIFY)
{
if (!$this->connected) {
$this->Connect();
}
$this->LastQueryString = $string;
$start = microtime(true);
$this->LastQuery = $this->Conn->query($string);
$finish = microtime(true) - $start;
$this->QueryTime += $finish;
$this->HitCount++;
if ($this->logQueries) {
$this->QueryLog .= "\r\nQuery #" . $this->HitCount . " Query duration: " . number_format($finish, 4);
if ($this->LastQuery) {
$this->QueryLog .= "\r\n\tRows: " . $this->LastQuery->rowCount();
} else {
$this->QueryLog .= "\r\n\tError: " . implode(' - ', $this->Conn->errorInfo());
}
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$function = !empty($trace) ? $trace[0] : array();
foreach ($trace as $function) {
if (array_key_exists('class', $function) && $function['class'] == 'MySQLDatabase') {
continue;
} else {
break;
}
}
$traceString = $function && array_key_exists('class', $function) ? $function['class'] . "::" : '';
$traceString .= $function && array_key_exists('function', $function) ? $function['function'] . '()' : '';
if (strlen($traceString)) {
$traceString .= "\r\n\t";
}
$traceString .= $function && array_key_exists('file', $function) ? $function['file'] . ':' . $function['line'] : '';
$this->QueryLog .= "\r\n\t{$traceString}";
if ($prettify) {
$formatter = new SqlFormatter();
$string = $formatter->format($string, false);
}
$this->QueryLog .= "\r\n\t------\r\n\t" . str_replace("\n", "\n\t", trim(str_replace("\t", ' ', $string), "\r\n")) . "\r\n\t------";
$this->QueryLog .= "\r\n";
}
return $returnID ? $this->Conn->lastInsertID() : $this->LastQuery;
}
示例2: catch
$connection2->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo $e->getMessage();
}
@session_start();
//Set timezone from session variable
date_default_timezone_set($_SESSION[$guid]["timezone"]);
$gibbonPersonID = $_POST["gibbonPersonID"];
$date = date("Y-m-d");
$type = "Positive";
$descriptor = "Quick Star";
$level = "";
$comment = "";
$gibbonPlannerEntryID = $_POST["gibbonPlannerEntryID"];
if ($gibbonPersonID == "" or $date == "" or $type == "" or $descriptor == "" or $gibbonPlannerEntryID == "") {
print _("Error");
} else {
//Write to database
try {
$data = array("gibbonPersonID" => $gibbonPersonID, "date" => $date, "type" => $type, "descriptor" => $descriptor, "level" => $level, "comment" => $comment, "gibbonPlannerEntryID" => $gibbonPlannerEntryID, "gibbonPersonIDCreator" => $_SESSION[$guid]["gibbonPersonID"], "gibbonSchoolYearID" => $_SESSION[$guid]["gibbonSchoolYearID"]);
$sql = "INSERT INTO gibbonBehaviour SET gibbonPersonID=:gibbonPersonID, date=:date, type=:type, descriptor=:descriptor, level=:level, comment=:comment, gibbonPlannerEntryID=:gibbonPlannerEntryID, gibbonPersonIDCreator=:gibbonPersonIDCreator, gibbonSchoolYearID=:gibbonSchoolYearID";
$result = $connection2->prepare($sql);
$result->execute($data);
} catch (PDOException $e) {
//Fail 2
$URL .= "&addReturn=fail2";
header("Location: {$URL}");
break;
}
print "<a href='" . $_SESSION[$guid]["absoluteURL"] . "/index.php?q=/modules/Behaviour/behaviour_manage_edit.php&gibbonBehaviourID=" . $connection2->lastInsertID() . "&gibbonPersonID=&gibbonRollGroupID=&gibbonYearGroupID=&type='><img style='margin-top: -30px; margin-left: 60px' src='" . $_SESSION[$guid]["absoluteURL"] . "/themes/" . $_SESSION[$guid]["gibbonThemeName"] . "/img/like_on.png'></a>";
}
示例3: createOrUpdate
/**
* Create or update an object in the database if it already exists
* @param array $update Values to update
* @param array $exclude Attributes not to set in create
* @param \PDO $db Database connection to use, default to master resource
* @return boolean
*/
public function createOrUpdate($update, $exclude = array(), &$db = FALSE)
{
// Get columns and values
$create = $this->createColumnsAndValues($exclude);
// Generate update values
$updateVals = NULL;
foreach ($update as $column => $value) {
// Add column
$updateVals .= ', `' . $column . '` = ';
// If the updated value is an array treat first item
// as literal query insert and the second a params to bind
$updateVals .= is_array($value) ? $value[0] : $value;
}
// Trim the first character (,) from the update
$updateVals = substr($updateVals, 2);
// Get database master for write
if ($db === FALSE) {
$db =& $this->getDbMaster();
}
// Prepare query
$query = $db->prepare('
INSERT INTO `' . static::$dbTable . '` (' . $create['columns'] . ')
VALUES ( ' . $create['values'] . ')
ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(' . static::$pk . '), ' . $updateVals . '
');
// Bind attributes
$this->bindAttributes($query, $exclude);
// Bind update parameters
foreach ($update as $value) {
if (is_array($value) && isset($value[1]) && is_array($value[1])) {
foreach ($value[1] as $column => $newVal) {
$query->bindValue($column, $newVal);
}
}
}
// Execute
if (!$this->executeCreateUpdateQuery($query)) {
return FALSE;
}
// Set the pk
if (!$this->attributeHasValue(static::$pk) || !$this->iget(static::$pk)) {
$this->iset(static::$pk, $db->lastInsertID());
}
// return TRUE
return TRUE;
}
示例4: PDO
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'mydb';
try {
$conn = new PDO("msql:host={$host};dbname={$dbname}", $user, $pass);
$statement = $conn->prepare("INSERT INTO finaldb(Gender, Birthdate, Texty) values (:gender, :bdat, :tex)");
$statement->bindValue(":gender", $_POST['gender']);
$statement->bindValue(":Birthdate", $_POST['bdat']);
$statement->bindValue(":Texty", $_POST['tex']);
$statement->execute();
$numRowsAffected = $statement->rowCount();
$insertedPrimaryKey = $conn->lastInsertID();
$conn = null;
header("Location: index.php");
} catch (PDOException $e) {
echo $e->getMessage();
}