如何显示行的值?

时间:2014-01-30 05:43:58

标签: php mysql class object mysqli

我在MySQLi数据库中设置了五行:

$title, $subtitle, $owner, $contactemail, and $footer.

我没有在每个.php页面上写出我的网站标题,而是想使用显示$ title,$ subtitle 等值的函数。

这是我到目前为止所做的:

的index.php

<?php include 'config.php'; ?>
<?echo $title;?>

的config.php

<?php
$mysql_hostname = 'localhost';
$mysql_username = 'root';
$mysql_dbname = 'toplist';
$mysql_password = '';

$dbh= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

$query = "SELECT * FROM toplist_settings";

$title=['title'];
$subtitle=['subtitle'];
$owner=['owner'];
$contactemail=['contactemail'];
$footer=['footer'];
?>

但这不起作用。当我使用时,没有任何东西出现。

2 个答案:

答案 0 :(得分:0)

试试这个

   $query = "SELECT * FROM toplist_settings";
   $stmt = $dbh->query($query); 
   $row = $stmt->fetch(PDO::FETCH_ASSOC);

    $title=$row['title'];
    $subtitle=$row['subtitle'];
    $owner=$row['owner'];
    $contactemail=$row['contactemail'];
    $footer=$row['footer'];

答案 1 :(得分:0)

执行查询以获取记录

  $query = $dbh->prepare("SELECT * FROM toplist_settings");  
  print_r( $query->execute());