2016-06-01 33 views
1

我在drupal的moodle數據庫中插入一行,並想獲取最後一個插入行的id,但我無處可去。 這是我的代碼。調用mysql_insert_id()在drupal中不工作

   $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(); 
+0

請訪問這個URL'https:// www.drupal.org/node/151718',可以幫助你。 –

回答

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 
+0

它不會將其識別爲函數 –

+0

您正在使用的是什麼版本的drupal。 –

+0

調用未定義的函數db_last_insert_id()這是我得到的響應 –

0

在Drupal 6,使用db_last_insert_id();

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

+0

什麼會根據我的代碼是語法 –

+0

我應該寫InsertQuery –

+0

的$ id = db_insert我插入查詢( 'MYTABLE') - >字段(陣列( '字段1'=> '值' )) - > execute(); –