繁体   English   中英

在PHP中获取SCOPE_IDENTITY()

[英]Get SCOPE_IDENTITY() in PHP

试图获取SCOPE_IDENTITY()(最后一个ID插入到DB中)并将其作为变量存储在我的PHP函数中。

看看我在堆栈溢出时可能找到的所有答案,我仍然没有设法到达那里。

这就是我目前拥有的:

// Confirm booking (update database) function
public function insert_userPost($conn) 
{
    // SQL INSERT command
    $sql = ("
           INSERT INTO userPost (content, submitted, emotion)
           VALUES ('$this->post', 'NOW()', '$this->emotion');
           ");
    if ($conn->query($sql) === TRUE) 
    {
        //echo "success";
        $sql = ("SELECT SCOPE_IDENTITY() as Id");
        $result = $conn->query($sql);
        echo $result;
        //header('Location: feed.php?filter=all&page=1');
    } 
    else 
    {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

我做错了什么?

编辑:

另外......在构造中我试图在$ this-> post中传递Id($ this-> Id),但它返回0.我可以看到这是因为我只是设置$在查询完成后,这个 - > Id,因此返回0,但我不确定如何继续。 有什么建议?

// Construct
public function __construct($content, $emotion, $conn)
{
    $this->content = $content;
    $this->emotion = $emotion;
    $this->post = 
        "<div id=\'post\'>
            <div id=\'postContent\'>
                <p><b>I\'m $this->emotion because</b> $this->Id $this->content<span class=\'emot\'id=\'$this->emotion\'></span></p>
            </div>
            <div id=\'postInfo\'>
                <span class=\'postRelate\' title=\'Click to relate +1\'><p><b>relate</b> (0)</p></span>
                <span class=\'postSubmitted\'><p>submitted X minutes ago</p></span>
            </div>
        </div>";     
}

// Confirm booking (update database) function
public function insert_userPost($conn) 
{
    // SQL INSERT command
    $sql = ("INSERT INTO userPost (content, submitted, emotion)
             VALUES ('$this->post', NOW(), '$this->emotion')");
    if ($conn->query($sql) === TRUE) 
    {
        //echo "success";
        echo $this->Id = $conn->insert_id;
        //header('Location: feed.php?filter=all&page=1');
    } 
    else 
    {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

首先,您的问题是标记为mysql ,但SCOPE_IDENTITY()是SQL Server函数。 话虽这么说,你的代码包含$ conn-> error ,所以我假设你正在使用带有MySQLi扩展的MySQL。

与SQL Server的SCOPE_IDENTITY()等效的MySQL是LAST_INSERT_ID() 但是调用它需要额外的查询,这很麻烦而且速度慢。

相反,建议您使用内置的MySQLi功能,连接实例的$ insert_id属性:

$id = $conn->insert_id;

大多数SQL库都具有内置功能。 如果您使用PDO作为数据库抽象层,则可以类似地使用PDO :: lastInsertId()

$id = $pdo->lastInsertId();

这适用于SQL Server和MySQL(以及其他)。

如果您使用的是MySQL,则需要使用LAST_INSERT_ID() ,这是SQLServer的等效项。

$sql = "SELECT LAST_INSERT_ID() as Id";

您还可以使用php属性$conn->insert_id

基于我对Scope_Identity的理解,您可以像这样使用它:

// Confirm booking (update database) function
public function insert_userPost($conn) 
{
    // SQL INSERT command
    $sql = ("
           INSERT INTO userPost (content, submitted, emotion)
           VALUES ('$this->post', 'NOW()', '$this->emotion');
           SELECT SCOPE_IDENTITY() as Id
           ");
    if ($conn->query($sql) === TRUE) 
    {
        $result = $sql;
        echo $result;
        //header('Location: feed.php?filter=all&page=1');
    } 
    else 
    {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

我不完全确定PHP语法,但SQL部分符合我自己的用法。 可能还有一个无关紧要的; 在SQL查询的第二行的末尾。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM