doushi2047
2016-05-11 18:29
浏览 50
已采纳

PHP / MYSQL创建表/插入数据[重复]

This question already has an answer here:

I'm actively learning mysql/php/arduino and trying to do some simple things to get me going. So far I have: created a database (works), created a table (doesn't work), inserted data (works), displayed data (doesn't work).

<?php
    $servername="localhost";
    $username="YUN";
    $password="12345678";
    $dbname = "yundatabasee";

    $conn = mysqli_connect($servername, $username, $password, $dbname);

    if(!$conn) {
        die("connessione fallita" .mysqli_connect_error());
    }

    $sql = "CREATE TABLE YunDati(
        id INT (6) UNSIGNED AUTO INCREMENT PRIMARY KEY,
        yunName VARCHAR (20) NOT NULL,
        digitalIn VARCHAR (2),
        analogIn VARCHAR (5),
        leggi_data TIMESTAMP
    )";

    if (mysqli_query($conn, $sql)) {
        echo "Tabella creata";
    } else {
        echo "Mi dispiace tabella non e' stata creata :P" .mysqli_error($conn);
    }

    mysqli_close($conn);
?>

Mi dispiace tabella non e' stata creata.

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AUTO INCREMENT PRIMARY KEY, yunName VARCHAR (20) NOT NULL, digitalIn VARCHAR (2)' at line 2

<?php
    $servername="localhost";
    $username="YUN";
    $password="12345678";
    $dbname = "yundatabasee";

    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn) {
        die("connessione fallita" .mysqli_connect_error());
    }


    $sql = "SELECT id, yunName,digitalIn, analogIn, leggi_data FROM YunDati";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result){
            echo"id : ".$row["id"]."- Nome".$row["yunName"]."-Analog In".$row["analogIn"]."<br>";
        }
    } else {
        echo "nessun risultato";
    }

    mysqli_close($conn);
?>

Parse error: syntax error, unexpected '{' in /opt/lampp/htdocs/YUNest/Mostradati.php on line 20

</div>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudou1855 2016-05-11 18:41
    已采纳

    So in your code, you have two sintax error, one in your SQL insert and other at your while loop:

    id INT (6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    

    AUTO_INCREMENT comes with an underscore in between, check this SQLfiddle, and :

    while ($row = mysqli_fetch_assoc($result)){  
    

    you have to remember to close all the brackets you open, these changes should fix your code.

    If you need a syntax checker in the future for php I would use phpcodechecker if you don't have a built-in one.

    Hope this helps you.

    打赏 评论

相关推荐 更多相似问题