创建数据库表不起作用。微软Access

问题描述:

目前我正在创建一个类似论坛的网站。 每次用户创建一个新的论坛主题时,我都希望它在我的数据库中创建一个新表格,这看起来无法实现。 我尝试了几种不同的方式,但简直无法让它工作。 在发布我的任何代码之前,请让我知道是否需要更多的代码,以便您了解我的问题。创建数据库表不起作用。微软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

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

CREATE TABLE MovingThreadTable1 ([text] text);