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

时间:2019-05-09 17:16:32

标签: php mysqli import mariadb

我正在尝试将文件导入数据库,无论我进行了什么更改,我一直收到相同的错误。 错误是-

您的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);
?>

2 个答案:

答案 0 :(得分:1)

请参阅添加的评论,以解释我所做的修改

<?php
// this needs a password, I assume yours in blank
$conn = mysqli_connect('localhost','root', '');

if (!$conn) {
    die(mysqli_error());
} 

// this creates a database
$status = mysqli_query($conn,"CREATE DATABASE IF NOT EXISTS monthly");

if ($status){
    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 ($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;

    // Column and databse names are wrapped in backticks
    // Text data is wrapped in single quotes
    // integer data CAN be wrapped in single quote, but does not have to be
    $qry = "insert into `month1` (`week1`,`week2`,`week3`,`week4`) 
                        values($week1,$week2,$week3,$week4)" 
    // This is placing a query in a string variable 

    // so this die() is nonsense and anyway it meeds a parameter in the mysqli_error()
    // like this mysqli_error($conn)
    //   or die(mysqli_error());

    // this execues the query above
    mysqli_query($conn,$qry);

    // if you made the above line into 
    // $res = mysqli_query($conn,$qry);
    // you could check if it actually worked like this
    /*
    if ( !$res ) {
        mysqli_error($conn);
    }
    */

}
fclose($open);
mysqli_close($conn);
?>
  

请参见When to use single quotes, double quotes, and backticks in MySQL

答案 1 :(得分:0)

我在代码的上部更改了几行,请检查比较,其中有些错误,例如

if (mysqli_query($conn,$db)) and if (mysqli_query($conn,$ct)){ 
 the above lines have no meaning.

请在您自己的位置添加以下代码。

<?php
$conn = mysqli_connect('localhost','root','');

if (!$conn) {
    die(mysqli_error());
} 

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

if ($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 ($ct){
    echo "Table created";
} else {
    echo "table not created: " . mysqli_error($conn);

}