致命错误:调用成员函数bindParam() [英] Fatal error: Call to a member function bindParam()
问题描述
我正在学习使用PHP并遇到一个简单的问题.
I am learning to work with PHP and have a simple problem.
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$db = new PDO('sqlite:/usr/users2/mieic2009/ei09072/public_html/TP1/Delicious /Database.db');
$a = $_GET['linkRef'];
$b = $_GET['tagToAdd'];
$checkIdLink = $db->prepare('SELECT idLink FROM Links WHERE nome_L = :link_n;');
$checkIdLink->bindParam(':link_n', $a, PDO::PARAM_STR);
$checkIdLink->execute();
$linkID = $checkIdLink->fetch();
$insertLink = $db->prepare('INSERT INTO Tags (nome_T, idLink) VALUES (:addTag, :link_id)');
$insertLink->bindParam(':addTag', $b, PDO::PARAM_STR);
$insertLink->bindParam(':link_id', $linkID, PDO::PARAM_INT);
$insertLink->execute();
echo 'Tag added to the specified link!';
?>
此代码应将标记添加到数据库中的现有链接,但是出现此错误
This code should add a Tag to an existing link in a database, however I am getting this error
致命错误:在非对象中调用成员函数bindParam() /usr/users2/mieic2009/ei09072/public_html/TP1/Delicious/addTag.php在 第9行
Fatal error: Call to a member function bindParam() on a non-object in /usr/users2/mieic2009/ei09072/public_html/TP1/Delicious/addTag.php on line 9
我已经一遍又一遍地检查了,但似乎找不到此代码的问题,我在Google上搜索了此错误,但不幸的是,我发现的答案不够有用.任何帮助将不胜感激,这可能是一个简单的菜鸟错误.
I have checked over and over and can't seem to find what's wrong with this code, I googled for this error but unfortunately the answer I found weren't helpful enough. Any help would be appreciated, this is probably a simple rookie mistake.
推荐答案
我将检查$db->prepare()
函数是否已成功执行.如果没有,它将返回false.因此,您可能试图在等于false
I would check that the $db->prepare()
function executed successfully. It will return false if it did not. So you could be trying to call bindParam()
on a variable that equals false
http://www.php.net/manual/en/pdo.prepare.php
此外,还应将PDO对象声明放在try/catch
中,以确保它也成功,如
Also you should put the PDO object declaration in try/catch
to be sure it was successful as well, as in the first example on this page:
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
这篇关于致命错误:调用成员函数bindParam()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!