创建表时出错

时间:2015-02-26 08:28:25

标签: php mysqli xampp ini

无论需要什么命令,我都从mysql更改为mysqli。当我运行它时,我收到以下错误:

  

创建表时出错:SQL语法中有错误;检查   手册,对应右边的MySQL服务器版本   语法使用附近'),)'在第7行

此通知:

  

注意:未定义的偏移量:C:\ XAMPP \ htdocs \ prog_db_mysqli.php中的4   第41行

和那个警告:

  

警告:mysqli_close()期望参数1为mysqli,boolean   在第47行的C:\ XAMPP \ htdocs \ prog_db_mysqli.php中给出

<?php
 require_once 'reader.php';
 $data = new Spreadsheet_Excel_Reader();
 $data->setOutputEncoding('CP1251');
 $data->read('prog.xls');

 $con = mysqli_connect("localhost","root","","mynewdb");
 // Check connection
 if (mysqli_connect_errno())
 {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 // Create table
 $sql="CREATE TABLE prog_scores(

      pid INT(1),
      pname VARCHAR(100),
      py TEXT(4),
      pcrit VARCHAR(100),
      pscore DOUBLE(100),
  )";

  // Execute query
 if (mysqli_query($con,$sql))
 {
     echo "Table prog_scores created successfully";
 }
 else
 {
      echo "Error creating table: " . mysqli_error($con);
 }

 for($x = 2; $x <= count($data->sheets[0]["cells"]); $x++)
 {
       //$sno = $data->sheets[0]["cells"][$x][1];
        $pname = $data->sheets[0]["cells"][$x][1];
        $py = $data->sheets[0]["cells"][$x][2];
        $pcrit = $data->sheets[0]["cells"][$x][3];
        $pscore = $data->sheets[0]["cells"][$x][4];



       $res = mysqli_query($con,"INSERT INTO prog_scores 
        (pname,pcrit,pscore,py) VALUES     ('$pname','$py','$pcrit','$pscore')");   
      mysqli_close($res);
  }
?>

我还检查了动态扩展中的php.ini文件,并且我从

更改了
         ;;;;;;;;;;;;;;;;;;;;;;
         ; Dynamic Extensions ;
         ;;;;;;;;;;;;;;;;;;;;;;

       ; If you wish to have an extension loaded automatically, use the       following
       ; syntax:
       ;
       ;   extension=modulename.extension
       ;
       ; For example, on Windows:
       ;
       ;   extension=msql.dll
       ;
       ; ... or under UNIX:
       ;
       ;   extension=msql.so
       ;
       ; ... or with a path:
       ;
       ;   extension=/path/to/extension/msql.so
       ;
       ; If you only provide the name of the extension, PHP will look for it in its
       ; default extension directory.
       ;
       ; Windows Extensions
       ; Note that ODBC support is built in, so no dll is needed for it.
       ; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
       ; extension folders as well as the separate PECL DLL download (PHP 5).
       ; Be sure to appropriately set the extension_dir directive.



       extension=php_bz2.dll
       extension=php_curl.dll
       extension=php_mbstring.dll
       extension=php_exif.dll
      ;extension=php_fileinfo.dll
       extension=php_gd2.dll
       extension=php_gettext.dll
      ;extension=php_gmp.dll
      ;extension=php_intl.dll
      ;extension=php_imap.dll
      ;extension=php_interbase.dll
      ;extension=php_ldap.dll
      ;extension=php_mssql.dll
      ;extension=php_mbstring.dll
      ;extension=php_exif.dll      ; Must be after mbstring as it depends on it
      extension=php_mysql.dll
      extension=php_mysqli.dll
     ;extension=php_oci8.dll      ; Use with Oracle 10gR2 Instant Client
     ;extension=php_oci8_11g.dll  ; Use with Oracle 11gR2 Instant Client

      extension=php_openssl.dll
     ;extension=php_pdo_firebird.dll
      extension=php_pdo_mysql.dll
     ;extension=php_pdo_oci.dll
     ;extension=php_pdo_odbc.dll
     ;extension=php_pdo_pgsql.dll
      extension=php_pdo_sqlite.dll
     ;extension=php_pdo_sqlite_external.dll
     ;extension=php_pgsql.dll
     ;extension=php_pspell.dll
     ;extension=php_shmop.dll

到这个

        ;;;;;;;;;;;;;;;;;;;;;;
         ; Dynamic Extensions ;
         ;;;;;;;;;;;;;;;;;;;;;;

       ; If you wish to have an extension loaded automatically, use the       following
       ; syntax:
       ;
       ;   extension=modulename.extension
       ;
       ; For example, on Windows:
       ;
       ;   extension=msql.dll
       ;
       ; ... or under UNIX:
       ;
       ;   extension=msql.so
       ;
       ; ... or with a path:
       ;
       ;   extension=/path/to/extension/msql.so
       ;
       ; If you only provide the name of the extension, PHP will look for it in its
       ; default extension directory.
       ;
       ; Windows Extensions
       ; Note that ODBC support is built in, so no dll is needed for it.
       ; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
       ; extension folders as well as the separate PECL DLL download (PHP 5).
       ; Be sure to appropriately set the extension_dir directive.

         extension=php_mysqli.dll

但它不起作用。我有PHP 5.5,我正在使用xampp。

3 个答案:

答案 0 :(得分:2)

您在插入后立即在循环内执行mysqli_close($res);,这意味着在循环的下一次迭代中连接已经关闭。

您需要在 循环之后关闭mysqli连接 ,而不是在其中

for($x = 2; $x <= count($data->sheets[0]["cells"]); $x++) {
    ....

    $res = mysqli_query($con,"INSERT INTO prog_scores 
        (pname,pcrit,pscore,py) VALUES
        ('$pname','$py','$pcrit','$pscore')");   
}
mysqli_close($res);

答案 1 :(得分:-2)

更改此

if (mysqli_connect_errno())
 {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

if (mysqli_connect_errno())
 {
    echo "Failed to connect to MySQL: " . mysqli_connect_errno();
 }

可能是你的第一个错误

答案 2 :(得分:-2)

我只是寻找同一个问题的答案,我真的没找到一个...... 实际上你的代码没有错,你应该知道这一点。 我相信也许某个地方有定义应该成功 以这种方式工作。

我只是试着发现你应该指定数据库名称 查询中的表名如下:

创建表格&#39; yourDatabaseName&#39;。&#39; yourTableName&#39; (等....)

这对我有用,直到我发现我可以在哪里定义它无需工作 指定数据库名称 如果有人有线索,请通知我。