[英]Error with a prepared statement with named placeholders in PHP with mySQL
使用此代碼,我的網頁上出現了這些錯誤
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行是此代碼中的第四行
<?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");
?>
您可能應該將connection
添加到數據庫或檢查connect.php
connection
設置是否正確
try
{
$dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.