0

我使用此代码在我的网页上收到这些错误

Notice: Undefined variable: dbh in /home/danu2_cj3/public_html/lists.php on line 14

Fatal error: Call to a member function prepare() on a non-object in /home/danu2_cj3/public_html/lists.php on line 14

这是我正在使用的 php 代码,第 14 行是此代码中的第 4 行

<?php
/* Execute a prepared statement by binding PHP variables */
$member = $_SESSION['SESS_MEMBER_ID'];
$sth = $dbh->prepare('SELECT name
    FROM lists
    WHERE member_id = :member_id ');
$sth->bindValue(':member_id', $member, PDO::PARAM_INT);
$sth->execute();
?>

我试图显示一堆行的数组,其中这些行中的一个字段应该与登录到会话的人的 member_id 匹配

这是页面的全部代码(top.php 和 bottom.php 只是布局页面)

<?php include ('top.php')?>
<h1>My Profile </h1>
<a href="lists.php">My Lists</a> | <a href="logout.php">Logout</a></br>
</br>
Please select list to view/delete
<?php include ("connect.php")?>
<?php
/* Execute a prepared statement by binding PHP variables */
$member = $_SESSION['SESS_MEMBER_ID'];
$sth = $dbh->prepare('SELECT name
    FROM lists
    WHERE member_id = :member_id ');
$sth->bindValue(':member_id', $member, PDO::PARAM_INT);
$sth->execute();
?>

<?php include ('bottom.php')?>

这是我的 connect.php 文件

<?php
$username = "mydb";
$password = "mydb";
$hostname = "host"; 

//connection to the database
$dbh = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to database");

//select a database to work with
$selected = mysql_select_db("mydb",$dbh) 
  or die("Could not select database");
?>
4

1 回答 1

0

您可能应该添加connection到数据库或检查它是否正确设置connect.php

try 
{
    $dbh = new PDO($dsn, $user, $password);
} 
catch (PDOException $e) 
{
    echo 'Connection failed: ' . $e->getMessage();
}

文档在这里

于 2013-04-15T19:49:58.503 回答