关于mysqli语法的php文件导入错误

我正在尝试将文件导入到数据库中,无论我做了什么更改,我都会收到相同的错误。错误是——


您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以获取在第 1 行的“1”附近使用的正确语法


似乎无法找到解决方案。我究竟做错了什么?谢谢 :)


<?php

$conn = mysqli_connect('localhost','root');


if (!$conn) {

    die(mysqli_error());


$db = mysqli_query($conn,"CREATE DATABASE IF NOT EXISTS monthly");

if (mysqli_query($conn,$db)){

    echo "Database created";

} else {

    echo "Database not created: " . mysqli_error($conn);


}


mysqli_select_db($conn, "monthly");


$ct = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS `month1`(

`week1` INT(4) NOT NULL,

`week2` INT(4) NOT NULL,

`week3` INT(4) NOT NULL,

`week4` INT(4) NOT NULL

)");

if (mysqli_query($conn,$ct)){

    echo "Table created";

} else {

    echo "table not created: " . mysqli_error($conn);


}


$open = fopen('/xampp/htdocs/month1.txt','r');


while (!feof($open)) 

{

    $getTextLine = fgets($open);

    $explodeLine = explode(',',$getTextLine, 4);


    if(count($explodeLine) !=4) {

        continue;

    }

    $week1 = $explodeLine[0];

    $week2 = $explodeLine[1];

    $week3 = $explodeLine[2];

    $week4 = $explodeLine[3];


    list($week1,$week2,$week3,$week4) = $explodeLine;



    $qry = "insert into 'month1' ('week1','week2','week3','week4') values('$week1','$week2','$week3','$week4')" or die(mysqli_error());

    mysqli_query($conn,$qry);

}

fclose($open);

mysqli_close($conn);

?>


Qyouu
浏览 156回答 2
2回答

临摹微笑

请参阅添加的评论以解释我所做的修改<?php// this needs a password, I assume yours in blank$conn = mysqli_connect('localhost','root', '');if (!$conn) {&nbsp; &nbsp; die(mysqli_error());}&nbsp;// this creates a database$status = mysqli_query($conn,"CREATE DATABASE IF NOT EXISTS monthly");if ($status){&nbsp; &nbsp; echo "Database created";} else {&nbsp; &nbsp; echo "Database not created: " . mysqli_error($conn);}mysqli_select_db($conn, "monthly");$ct = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS `month1`(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `week1` INT(4) NOT NULL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `week2` INT(4) NOT NULL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `week3` INT(4) NOT NULL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `week4` INT(4) NOT NULL&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )");if ($ct){&nbsp; &nbsp; echo "Table created";} else {&nbsp; &nbsp; echo "table not created: " . mysqli_error($conn);}$open = fopen('/xampp/htdocs/month1.txt','r');while (!feof($open))&nbsp;{&nbsp; &nbsp; $getTextLine = fgets($open);&nbsp; &nbsp; $explodeLine = explode(',',$getTextLine, 4);&nbsp; &nbsp; if(count($explodeLine) !=4) {&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; $week1 = $explodeLine[0];&nbsp; &nbsp; $week2 = $explodeLine[1];&nbsp; &nbsp; $week3 = $explodeLine[2];&nbsp; &nbsp; $week4 = $explodeLine[3];&nbsp; &nbsp; list($week1,$week2,$week3,$week4) = $explodeLine;&nbsp; &nbsp; // Column and databse names are wrapped in backticks&nbsp; &nbsp; // Text data is wrapped in single quotes&nbsp; &nbsp; // integer data CAN be wrapped in single quote, but does not have to be&nbsp; &nbsp; $qry = "insert into `month1` (`week1`,`week2`,`week3`,`week4`)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values($week1,$week2,$week3,$week4)"&nbsp;&nbsp; &nbsp; // This is placing a query in a string variable&nbsp;&nbsp; &nbsp; // so this die() is nonsense and anyway it meeds a parameter in the mysqli_error()&nbsp; &nbsp; // like this mysqli_error($conn)&nbsp; &nbsp; //&nbsp; &nbsp;or die(mysqli_error());&nbsp; &nbsp; // this execues the query above&nbsp; &nbsp; mysqli_query($conn,$qry);&nbsp; &nbsp; // if you made the above line into&nbsp;&nbsp; &nbsp; // $res = mysqli_query($conn,$qry);&nbsp; &nbsp; // you could check if it actually worked like this&nbsp; &nbsp; /*&nbsp; &nbsp; if ( !$res ) {&nbsp; &nbsp; &nbsp; &nbsp; mysqli_error($conn);&nbsp; &nbsp; }&nbsp; &nbsp; */}fclose($open);mysqli_close($conn);?>

拉丁的传说

我正在更改您代码上部的几行,请检查并比较,有一些小错误,例如if (mysqli_query($conn,$db)) and if (mysqli_query($conn,$ct)){&nbsp;&nbsp;the above lines have no meaning.请在该位置或您的位置添加以下代码。<?php$conn = mysqli_connect('localhost','root','');if (!$conn) {&nbsp; &nbsp; die(mysqli_error());}&nbsp;$db = mysqli_query($conn,"CREATE DATABASE IF NOT EXISTS monthly");if ($db){&nbsp; &nbsp; echo "Database created";} else {&nbsp; &nbsp; echo "Database not created: " . mysqli_error($conn);}mysqli_select_db($conn, "monthly");$ct = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS `month1`(`week1` INT(4) NOT NULL,`week2` INT(4) NOT NULL,`week3` INT(4) NOT NULL,`week4` INT(4) NOT NULL)");if ($ct){&nbsp; &nbsp; echo "Table created";} else {&nbsp; &nbsp; echo "table not created: " . mysqli_error($conn);}
打开App,查看更多内容
随时随地看视频慕课网APP