0

I'm a beginner and I'm trying to view my database. Please help me solve it.

This is the error shown:

Parse error: syntax error, unexpected 'Date' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\SLR\ViewData.php on line 20

This is my code:

<?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();
?>
Alexander
  • 3,922
  • 7
  • 24
  • 34
Steven Surain
  • 41
  • 2
  • 3
  • 5

2 Answers2

0

Three things : you have a typo in the $row["Expiry Date"] which should be $row['expiry_date']in order to match your query

and second: as mentioned by @Rahul Vyas - use single quotes inside your echoed variables :

and third -You have a few typos with your concatenation in your echo statement .

 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>"

This is causing the error as shown in the error message - it is expecting a Date when what you are trying to do is echo a string. Essentially you have to put quotes after every concatenation so that the echo works. The code should be:

 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>";
gavgrif
  • 13,077
  • 2
  • 17
  • 22
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();
?>

Use single quotes in array and you did not write correct column name 'expiry_date'.

Rahul Vyas
  • 261
  • 7
  • 18
  • hiya - whilst you have provided the answer, it would be good for teaching purposes to explain to the OP what was wrong and what you changed to correct the errors rather than just posting the corrected code. Thats why I tried to break my answer up into sections anddidn't just copy out the original code and correct it. Better for the OP to take on the corrections and make the changes himself. – gavgrif Apr 09 '16 at 10:23
  • 1
    but we are the learned professionals and we have a responsibility to share the knowlede and to educate the inexperienced to enable them to learn from the pool of SO knowledge.... – gavgrif Apr 09 '16 at 10:29
  • @gavgrif Thanks for such great advice I will keep this in mind in future :) – Rahul Vyas Apr 09 '16 at 10:31