0

im trying to insert data from MySQL into a table however I can't seem to get the loop working correcty :(

The select statement is correct, I just cant get it to echo the data out correctly.

Any idea?

Edited code is below which I fixed :)

There error is

Parse error: syntax error, unexpected '(', expecting T_STRING or T_VARIABLE or '{' or '$' in /home/4507408/public_html/review.php on line 67

.

<?php

$sql  = "SELECT u.userID, p.productID, p.name, c.categoryname, r.reviewID, r.reviewTime, r.reviewData, u.firstname FROM review r INNER JOIN product p on p.productID = r.productID INNER JOIN user u on u.userID = r.userID INNER JOIN category c on c.categoryID = p.categoryID ORDER BY $sortby ASC;";

$query = $DBH->prepare($sql);
$query->execute();
$data = $query->fetchALL();
$originalDate = $row['reviewTime'];
$newDate = date("d-m-Y", strtotime($originalDate));
while($row = $data->(PDO::FETCH_ASSOC))                     
{

?>

<table class="rev">

<tr>
<th><a href="review.php?sort=name">Product Name:</a></th>
<th><a href="review.php?sort=categoryname">Category</a></th>
<th><a href="review.php?sort=reviewID">ID</a></th>
<th><a href="review.php?sort=reviewTime">Date</a></th>
<th><a href="review.php?sort=reviewData">Review</a></th>
<th><a href="review.php?sort=firstname">Reviewer's Name</a></th>
</tr>


<td><a href="viewproduct.php?productID=<?=$row['productID']?>"><?=$name?></a></td>
<td><?=$row['categoryname']?></td>
<td><?=$row['reviewID']?></td>
<td><?=$newDate?></td>
<td><?=$row['reviewData']?></td>
<td><?=$row['firstname']?></td>
</tr>

<?      } ?>

CORRECT CODE:

                                                <?php

                                                $sql  = "SELECT u.userID, p.productID, p.name, c.categoryname, r.reviewID, r.reviewTime, r.reviewData, u.firstname FROM review r INNER JOIN product p on p.productID = r.productID INNER JOIN user u on u.userID = r.userID INNER JOIN category c on c.categoryID = p.categoryID ORDER BY $sortby ASC;";

                                                $query = $DBH->prepare($sql);
                                                $query->execute();
                                                $data = $query->fetchALL();

                                                ?>

                                    <table class="rev">

                                                <tr>
                                                <th><a href="review.php?sort=name">Product Name:</a></th>
                                                <th><a href="review.php?sort=categoryname">Category</a></th>
                                                <th><a href="review.php?sort=reviewID">ID</a></th>
                                                <th><a href="review.php?sort=reviewTime">Date</a></th>
                                                <th><a href="review.php?sort=reviewData">Review</a></th>
                                                <th><a href="review.php?sort=firstname">Reviewer's Name</a></th>
                                                </tr>

                                                <? foreach ($data as $row): ?>
                                                <?
                                                $originalDate = $row['reviewTime'];
                                                $newDate = date("d-m-Y", strtotime($originalDate));
                                                ?>
                                                <tr>
                                                    <td><a href="viewproduct.php?productID=<?=$row['productID']?>"><?=$row['productID']?></a></td>
                                                    <td><?=$row['categoryname']?></a></td>
                                                    <td><?=$row['reviewID']?></a></td>
                                                    <td><?=$newDate?></a></td>
                                                    <td><?=$row['reviewData']?></a></td>
                                                    <td><?=$row['firstname']?></a></td>
                                                </tr>


                                                <? endforeach?>


                                        </table>

3 Answers 3

1

This line doesn't make any sense:

while($row = $data->(PDO::FETCH_ASSOC))  

I think what you need (based on the docs) is

foreach ($data as $row)

since fetchAll returns an array.

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

Comments

0

@jon is correct you need a foreach loop and you need to place PDO::FETCH_ASSOC into $query->fetchAll(PDO::FETCH_ASSOC)

Comments

0

it should be,

 $data = $query->fetchALL();
 foreach($data as $row)    

instead of

  while($row = $data->(PDO::FETCH_ASSOC))  

Also,

<?php  } ?>

not until you have not configure the shortag

<?   } ?>

2 Comments

Thanks I have solved it now and updated the question with correct code.
In the process of transferring over to PDO

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.