1

I have ran into a problem that says:

Catchable fatal error: Object of class PDOStatement could not be converted to string in F:\University\xampp\htdocs\database.php on line 36

Connection to a database is successful it is just that I cannot retrieve a data from my database and display it on the page.

Here's my database:

http://screenshot.sh/m1Ol9a8Fp2j3a

And here is my code

    <?php
$conn = new PDO(
    'mysql:host=localhost;dbname=u1358595', 
    'root'
    );
try{
       $conn = new PDO('mysql:host=localhost;dbname=u1358595', 'root');
    echo "Connected successfully"; 
}

catch (PDOException $exception) 
{
    echo "Oh no, there was a problem" . $exception->getMessage();
}
$query = "SELECT * FROM hotel";
$results = $conn->query($query, PDO::FETCH_OBJ);
$hotel = $results->fetch();
echo "<p>".$results."</p>";
$conn=NULL;
?>
2
  • Move the PDO::FETCH_OBJ to fetch $hotel = $results->fetch( PDO::FETCH_OBJ); Commented Nov 5, 2015 at 20:01
  • still gives me that error message ;/ Commented Nov 5, 2015 at 20:06

1 Answer 1

3

You are trying to echo a PDO Object which doesn't work. You are passing the data to $hotel and to show the data you will use hotel.

<?php
$conn = new PDO(
    'mysql:host=localhost;dbname=u1358595', 
    'root'
    );
try{
       $conn = new PDO('mysql:host=localhost;dbname=u1358595', 'root');
    echo "Connected successfully"; 
}

catch (PDOException $exception) 
{
    echo "Oh no, there was a problem" . $exception->getMessage();
}
$query = "SELECT * FROM hotel";
$results = $conn->query($query);
$hotel = $results->fetch();

echo '<pre>';
var_dump($hotel);
echo '</pre>';

$conn=NULL;
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you that works, how about if I want to get rid of this and only show the actual information from the table/ array(16) { ["hotel_id"]=> string(1) "1" [0]=> string(1) "1" ["name"]=> string(11) "The Midland" [1]=> string(11) "The Midland" ["address"]=> string(11) "16 Peter St" [2]=> string(11) "16 Peter St" ["postcode"]=> string(7) "M60 2DS" [3]=> string(7) "M60 2DS" ["town"]=> string(10) "Manchester" [4]=> string(10) "Manchester" ["description"]=>
You can display individual items from the array like echo $hotel['name']; and echo $hotel['address'];
So I want the information from the table only, not the column names etc:screenshot.sh/m2d6HUP3VBTpi

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.