1

I have the following script that should show the results of the MySQL query and populate the table below but it does the table comes out empty. I know there should be results shown, but I am unsure where the syntax issue is:

<table>
<tr>
    <th>Country</th>
    <th>Part</th>
    <th>Description</th>
    <th>Quantity</th>
    <th>Ship_Date</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "username", "password", "dbname");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "
  SELECT t1.Country
     , t2.Part
     , t2.Description
     , Sum(t2.Quantity) AS Quantity
     , t1.ship_time
  FROM Table1 t1
  JOIN Table_Data2 t2 
    ON t1.CodeValue = t2.CodeValue
 WHERE t2.Part IN ('BB1234', 'BB-3454')
   AND t1.ship_time = Current_Date()
 GROUP 
    BY t1.Country
     , t2.Part
     , t2.Description
     , t1.ship_time;
";

$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["t1.Country"].
         "</td><td>".$row["t2.Part"]. 
         "</td><td>". $row["t2.description"]. 
         "</td><td>" .$row["t2.quantity"].
         "</td><td>". $row["t1.ship_time"]. "</td><td>";
    }
    echo "</table>";
} else { 
    echo "0 results"; 
}
$conn->close();
?>
</table>

No error messages come up but I suspect the error may be when I do echo "<tr><td>".$row["t1.Country"]. for each column

12
  • 1
    Is your error reporting on? Commented Oct 1, 2019 at 11:48
  • No error messages come up but I suspect the error may be when I doecho "<tr><td>".$row["t1.Country"]. for each header Commented Oct 1, 2019 at 11:49
  • Is your database's name "dbname"? And your username is "username" and password is "password"? Commented Oct 1, 2019 at 11:49
  • I have removed the actual value for this example Commented Oct 1, 2019 at 11:50
  • 1
    instead of t1.Country try 'Country' also keep in mind that table associations are case sensitive, so Country != country, you can just print_r( $row ) and see if values are appearing with the keys you're mentioning... and also it should be $row = mysqli_fetch_assoc( $result ) if I'm not mistaken Commented Oct 1, 2019 at 11:58

1 Answer 1

1
echo "<tr><td>".$row["t1.Country"]

You should replace like below:

echo "<tr><td>".$row["Country"]

And remove the t1 and t2 from all the other as well

Sign up to request clarification or add additional context in comments.

1 Comment

You could also mention that the case is wrong on some of the other references like Description !== description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.