2015-06-21 130 views
0

目前我正在創建一個類似論壇的網站。 每次用戶創建一個新的論壇主題時,我都希望它在我的數據庫中創建一個新表格,這看起來無法實現。 我嘗試了幾種不同的方式,但簡直無法讓它工作。 在發佈我的任何代碼之前,請讓我知道是否需要更多的代碼,以便您瞭解我的問題。創建數據庫表不起作用。微軟Access

當運行功能我得到這個錯誤:

function createForumMovingThreadsTable($movingThreadTableName){ 
    $sql = "CREATE TABLE MovingThreadTable1 (text text);"; 
    sql($sql); 
} 

SQL函數:

function sql($query){ 
    $conn = new COM ("ADODB.Connection") or die("Cannot start ADO"); 
    //$connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\wwwWEB\\www\\2014-2015\\WEB14_49\\test\\userdb.mdb"; 
    $root = $_SERVER["DOCUMENT_ROOT"]; 
    $connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=$root\\2014-2015\\WEB14_13\\db\\userdb.mdb"; 

    $conn->open($connStr); 
    try 
    { 
     $rs = $conn->execute($query); 

     # Assosiative array 
     $assoc_array = array(); 

     # Number 
     $index = 0; 

     # The below code will create an array (depending on the fields of the database table of course): 
     # Click to see the array created by the code below 
     if ($rs->State) { 
      # Iterate through our result 
      while (!$rs->EOF) { 

       for ($x = 0; $x < $rs->Fields->Count; $x++) { 
        $assoc_array[$index][$rs->Fields[$x]->Name] = $rs->Fields[$x]->Value; 
       } 
       # Move cursor to next row in recordset 
       $rs->MoveNext(); 
       $index++; 
      } 
     } 

     $conn->Close(); 
     return $assoc_array; 
    } catch (Exception $e) { 
     $conn->Close(); 
     echo 'Caught exception: ', $e->getMessage(), "\n"; 
     echo $query; 
    } 
    return false; 
} 

告訴我,如果需要更多的信息,創建表

Caught exception: Source: Microsoft JET Database Engine 
Description: Syntax error in field definition. CREATE TABLE MovingThreadTable1 (text text); 
Warning: Cannot modify header information - headers already sent by (output started at D:\wwwWEB\www\2014-2015\WEB14_13\dbtools.php:39) in D:\wwwWEB\www\2014-2015\WEB14_13\createMovingThread.php on line 18 

功能。

+1

順便說一下,這不是一個最佳策略。線程實際上可能是無限的,因此你的代碼迫使無限的數據庫重組(即創建表)。另外,表名將在下次運行代碼時存在。此外,該表不保留索引id,如用於引用的「userid」。爲什麼不維護一個表「MovingThreads」並相應地運行插入/更新/刪除? – Parfait

回答

0

文本是MS Access Jet/ACE引擎中的保留字。考慮包圍字段名稱或完全更改字段名稱。

CREATE TABLE MovingThreadTable1 ([text] text);