MySQL返回一个空结果集

时间:2015-01-06 14:36:14

标签: php mysql

我不明白为什么mysql表返回空结果集,代码似乎没问题。 来自mysql的响应:MySQL返回一个空结果集(即零行)。 (查询耗时0.0010秒)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "myexcel";

// Create connection
$connection = mysqli_connect($servername, $username, $password, $database);


if (isset($_POST['submit']))
{
        $file = $_FILES['file']['tmp_name'];
        $handle = fopen( $file, "r");
        while(($fileop = fgetcsv($handle, 3000, ",")) !== false)
        {
            $id = $fileop[0];
            $name = $fileop[1];
            $sql = "INSERT INTO csv (id, name) VALUES ('$id', '$name')";
        }

        if ($sql)
        {
            echo "OK";
        }
}
?>

<html>
<body>
 <input type="file" name="file" id="file"> 
 <input type="submit" value="Upload" name="submit">
</body>
</html>

2 个答案:

答案 0 :(得分:3)

您忘了查询:

$sql = mysqli_query($connection, "INSERT INTO csv (id, name) VALUES ('$id', '$name')");

答案 1 :(得分:0)

添加mysqli_query并将if condition置于while循环内....我在代码中看不到任何form标记???我在这里添加了post method

   ?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database = "myexcel";

    // Create connection
    $connection = mysqli_connect($servername, $username, $password, $database);


    if (isset($_POST['submit']))
    {
            $file = $_FILES['file']['tmp_name'];
            $handle = fopen( $file, "r");
            while(($fileop = fgetcsv($handle, 3000, ",")) !== false)
            {
                $id = $fileop[0];
                $name = $fileop[1];
                $sql = mysqli_query($connection, "INSERT INTO csv (id, name) VALUES ('$id', '$name')");

            if ($sql)
            {
                echo "Ok. Inserted <br>";
            }
            }


    }
    ?>

    <html>
    <body>
      <form method="POST"> // add this
     <input type="file" name="file" id="file"> 
     <input type="submit" value="Upload" name="submit">
    </form>
    </body>
    </html>