0

I am able to retrieve and display one row of data from a database however I would like to retrieve more than one. Code I have is messed up but I think I am going in the right area just need to make this work.

My Code:

    $query = "SELECT * FROM hotel";
$results = $conn->query($query);
    while($hotel = $results->fetch()) { echo "<br> id: ". $hotel['hotel_id']. " - Name: ". $hotel['name']. " - Address: " . $hotel['address'] . " - Postcode: " .
$hotel['postcode']. " - Town: " . $hotel['town']. " - Description: ". $hotel['description']. " - rating: ". $hotel['rating']. " - image: " . <img src='". $hotel['image']. "'>"";
    }
$conn=NULL;
?>
1
  • a result of how you when you execute your SQL query out of the code ? Commented Nov 6, 2015 at 13:35

3 Answers 3

1

There are a few things wrong with your code:

$query = "SELECT * FROM hotel";
$results = $conn->query($query);
$hotel = $results->fetch();

At this point, you've loaded the first record into $hotel. You don't need to do this if you're wanting to iterate through the recordset.

$hotel[$id] = $hotel['hotel_id'];
$hotel[$name] = $hotel['name'];
$hotel[$address] = $hotel['address'];
$hotel[$postcode] = $hotel['postcode'];
$hotel[$town] = $hotel['town'];
$hotel[$description] = $hotel['description'];
$hotel[$rating] = $hotel['rating'];
$hotel[$image] = $hotel['image'];

These lines are somewhat redundant. You've already got the details of the row in $hotel, and you're adding extra entries using array keys that have not been defined.

/*
$id = $hotel['hotel_id'];
$name = $hotel['name'];
$address = $hotel['address'];
$postcode = $hotel['postcode'];
$town = $hotel['town'];
$description = $hotel['description'];
$rating = $hotel['rating'];
$image = $hotel['image'];
*/
while($hotel = $results->fetch()) {
    echo "<br> id: ". $hotel[$id] = $hotel[id]. " - Name: ". $hotel[$name]. " - Address: "     . $hotel[$address] . " - Postcode: " . $hotel[$postcode].
    " - Town: " . $hotel[$town]. " - Description: ". $hotel[$description]. " - rating: ". $hotel[$rating]. " - image: " . $hotel[$image];
}

Inside your loop, you're trying to access array keys incorrectly. $hotel[$id] is using $id as the key, and that variable doesn't exist. $hotel[id] is using an unquoted key, which PHP will guess is $hotel['id'], but will complain about. So you can avoid the warning by quoting it directly.

So you can simplify your code down to:

$query = "SELECT * FROM hotel";
$results = $conn->query($query);

while($hotel = $results->fetch()) {
    echo "<br> id: ". $hotel['id'] . " - Image: <img src='". $hotel['image']. "'>"  ....
}
Sign up to request clarification or add additional context in comments.

8 Comments

Ok looks fine, doesn't show a image, how would i tackle this? and also it only shows a row with id: 2 and ignores the one with id:1 and in my database I have this screenshot.sh/odX3fHeLnBJ7O
If you want an image, you'll have to add the HTML markup and wrap it in an <img> tag. The above code will show everything in the database table, and it won't skip any of the records. Are you using the exact code, having removed all your existing code?
I am sure you can't do that inside php tags however it needs to be inside that while loop right? updated my orginal post with new code.
@PrzemekWojtas - You've got $hotel = $results->fetch(); before the loop. That is why you're missing the first entry. Delete that line.
@PrzemekWojtas - I've tweaked the code a touch so the image should show up more or less correctly.
|
0

I see that you're using PDO. You can simply iterate through the results like this.

while($results_disp = $results->fetch(PDO::FETCH_OBJ))
{
//access rows here in this manner
echo "Hotel Id :- " . $results_disp->id;
}

4 Comments

Still gives me these 2 errors ;/ I have edited previous post
Yeah, looks like it now, look at previous post
Nvm got it however it displays id: 2 while ignoring id number 1
Should't have. Mark the answer as accepted if it helped you.
0
 while($hotel = $results->fetch()) {
    echo "<br> id: ". $hotel['id']. " - Name: ". $hotel['name']. " - Address: " . $hotel['address'] . " - Postcode: " . $hotel['postcode'].
    " - Town: " . $hotel['town']. " - Description: ". $hotel['description']. " - rating: ". $hotel['rating']. " - image: " . $hotel['image'];
}

Replace your while loop with this .

5 Comments

Right wait emm which "$" I need to remove?
you need to edit the following: $hotel[$id] = $hotel[id], $hotel[$name] = $hotel[name], $hotel[address] and so on
@PrzemekWojtas - edit your question, and add in the current version of the code you're using, please.
Replace your while with this : while($hotel = $results->fetch()) { echo "<br> id: ". $hotel['id']. " - Name: ". $hotel['name']. " - Address: " . $hotel['address'] . " - Postcode: " . $hotel['postcode']. " - Town: " . $hotel['town']. " - Description: ". $hotel['description']. " - rating: ". $hotel['rating']. " - image: " . $hotel['image']; } Use this
Notice: Undefined property: PDOStatement::$id in F:\University\xampp\htdocs\assignment1\database.php on line 58 This is what I get now everything else is fine and that line is where if statement starts

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.