我试图查看我的数据库,但有错误

时间:2016-04-09 10:03:34

标签: php mysql

我是初学者,我正在尝试查看我的数据库。请帮我解决一下。

这是显示的错误:

  

解析错误:语法错误,意外'日期' (T_STRING),期待','或';'在第20行的C:\ xampp \ htdocs \ SLR \ ViewData.php

这是我的代码:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "slr";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT soft_id, soft_name, installed_date, expiry_date, product_key FROM software";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Software ID: " . $row["soft_id"]. " - Software Name: " . $row["soft_name"]. - Installed Date: " . $row["installed_date"].- Expiry Date: " . $row["Expiry Date"].- Product Key: " . $row["product_key"]."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

2 个答案:

答案 0 :(得分:0)

三件事:$row["Expiry Date"]中应该有一个拼写错误  $row['expiry_date']以匹配您的查询

和第二:正如@Rahul Vyas所提到的 - 在回音变量中使用单引号:

和第三个 - 你的echo语句中有一些拼写错误。

 echo "Software ID: " . $row["soft_id"]. " - Software Name: " . $row["soft_name"]. - Installed Date: " . $row["installed_date"].- Expiry Date: " . $row["Expiry Date"].- Product Key: " . $row["product_key"]."<br>"

这导致错误,如错误消息中所示 - 当您尝试执行的操作是回显字符串时,它正在等待Date。基本上你必须在每次连接后加上引号,以便回声起作用。代码应该是:

 echo "Software ID: " . $row['soft_id']. " - Software Name: " . $row['soft_name']. " - Installed Date: " . $row['installed_date']. "- Expiry Date: " . $row['expiry_date']. "- Product Key: " . $row['product_key']."<br>";

答案 1 :(得分:0)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "slr";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT soft_id, soft_name, installed_date, expiry_date, product_key FROM software";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Software ID: " . $row['soft_id']. " - Software Name: " . $row['soft_name']. "- Installed Date: " . $row['installed_date']."- Expiry Date: " . $row['expiry_date']."- Product Key: " . $row['product_key']."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

在数组中使用单引号,但您没有写出正确的列名&#39; expiry_date&#39;。