调用mysql_insert_id()不能在drupal中工作

时间:2016-06-01 05:37:24

标签: php mysql drupal-7

我在drupal的moodle数据库中插入一行,并希望得到最后一行插入的id,但我无处可去。 这是我的代码。

                $moodle_database = array(
                      'database' => 'moood',
                      'username' => 'root',
                      'password' => '',
                      'host' => 'localhost', //52.23.83.176
                      'driver' => 'mysql',
                    );

                    Database::addConnectionInfo('moodle_db', 'default', $moodle_database);
                    db_set_active('moodle_db');


                    try {

                    $question_query ="INSERT INTO mdl_question ".
                       "(category,parent,name,questiontext,generalfeedback) ".
                       "VALUES ".
                       "('$category','$parent','$name','$questiontext','$generalfeedback')";
                        $queryResult=db_query($question_query);
                        $id = mysql_insert_id($queryResult);
                      }

                    catch (Exception $e) {
                      drupal_set_message(t('Exception while fetching question data ' . $e->getMessage()));
                    }

                    db_set_active();

2 个答案:

答案 0 :(得分:0)

如果你使用Drupal 6:

,你应该使用下面的代码
db_last_insert_id('mdl_question', 'your_primary_key_id');

如果使用Drupal 7,请参阅以下链接:

http://drupal.stackexchange.com/questions/58991/how-to-retrieve-last-mysql-insert-id-from-drupal-7-db-merge

答案 1 :(得分:0)

在drupal 6中,使用db_last_insert_id();

在Drupal 7 InsertQuery::execute()中返回last_insert_id。