简体   繁体   English

在PhP中从MySql检索和显示数据

[英]Retrieving and displaying data from MySql in PhP

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 可捕获的致命错误:无法将类PDOStatement的对象转换为第36行的F:\\ University \\ xampp \\ htdocs \\ database.php中的字符串

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 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;
?>

You are trying to echo a PDO Object which doesn't work. 您正在尝试回显不起作用的PDO对象。 You are passing the data to $hotel and to show the data you will use hotel. 您将数据传递给$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;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM