【问题标题】:SQLite Auto increment field via PHP通过 PHP 的 SQLite 自动增量字段
【发布时间】:2017-09-09 17:21:14
【问题描述】:

我有这个当前的 SQLite 表:

CREATE TABLE Goal (
  id INTEGER PRIMARY KEY,
  Name CHAR(32) NOT NULL,
  Image CHAR(256),
  Target INTEGER NOT NULL,
  Priority CHAR(32) NOT NULL
);

我正在尝试通过 PHP 创建一个整数 id,如下所示:

    $query = 'INSERT OR IGNORE INTO Goal (Name, Image, Target, Priority) VALUES'; // Query to insert goal-related info into Goal table
    $query .= "('" . $name . "', '" . $image . "', '" . $target . "', '" . $priority . "');"; 
    $addGoalDB -> makeQuery( $query ); 

    $query = 'SELECT last_insert_rowid()';
    $result = $addGoalDB -> makeQuery( $query );
    $id = $result -> fetchArray(SQLITE3_ASSOC);
    $id = $id['id'];

    $query = 'INSERT OR IGNORE INTO Users_Goal (Email, id) VALUES'; 
    $query .= "('" . $_SESSION['username'] . "', '" . $id . "');"; 
    $addGoalDB -> makeQuery( $query ); 

但使用

不断收到未定义的索引错误
$id = $id['id'];

我假设查询是错误的,但找不到我的问题所在。此链接:http://alvinalexander.com/android/sqlite-autoincrement-insert-value-primary-key 使它看起来很简单,但不适用于我的代码。对此我将不胜感激。

【问题讨论】:

    标签: php database sqlite


    【解决方案1】:

    您正在尝试读取名为“id”的列。但是列是以您在SELECT 子句中编写的内容命名的,因此实际的列名称类似于last_insert_rowid()

    给列一个合适的名称:

    $query = 'SELECT last_insert_rowid() AS id';
    

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 2017-03-07
      相关资源
      最近更新 更多