php pdo - 在非对象上调用成员函数 prepare()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5346186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pdo - Call to a member function prepare() on a non-object
提问by anvd
This code get an error:
此代码出现错误:
Fatal error: Call to a member function prepare() on a non-object in C:\Users\fel\VertrigoServ\www\login\validation.php on line 42
致命错误:在第 42 行的 C:\Users\fel\VertrigoServ\www\login\validation.php 中的非对象上调用成员函数 prepare()
CODE:
代码:
function repetirDados($email) {
if(!empty($_POST['email'])) {
$query = "SELECT email FROM users WHERE email = ?";
$stmt = $pdo->prepare($query); // error line: line 42
$email = mysql_real_escape_string($_POST['email']);
$stmt->bindValue(1, $email);
$ok = $stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($results == 0) {
return true;
} else {
echo '<h1>something</h1>';
return false;
}
}
}
What is the possible cause? Another question, What is the equivalent to mysql_num_rows
? sorry, I am newbie with pdo
可能的原因是什么?另一个问题,相当于mysql_num_rows
什么?对不起,我是 pdo 的新手
回答by meagar
$pdo
is undefined. You're not declaring it inside the function, and it isn't being passed in as an argument.
$pdo
未定义。您没有在函数内部声明它,也没有将它作为参数传入。
You need to either pass it in (good), or define it in the global namespace and make it available to your function by placing global $pdo
at the top (bad).
您需要将它传入(好),或者在全局命名空间中定义它,并通过放置global $pdo
在顶部(坏)使其可用于您的函数。
回答by M Alam Telot
You can make a function of database connection in the same php page & call that function whenever you want. As,
您可以在同一个 php 页面中创建数据库连接功能,并随时调用该功能。作为,
public function connection()
{
$dbc = new PDO("mysql:host=localhost;dbname=chat","root","");
}
public function1()
{
this->connection();
// now you have the connection.. now, time for to do some query..
}
public function2()
{
this->connection();
// now do query stuffs..
}
Or simply you can simply write the database connection line in that page every time when you need it. As,
或者干脆您可以在每次需要时在该页面中简单地编写数据库连接行。作为,
public function a()
{ // connecting DB for this function a only...
$dbc = new PDO("mysql:host=localhost;dbname=chat","root","");
// bla bla bla...
}
public function b()
{ // connecting DB for this function b only...
$dbc = new PDO("mysql:host=localhost;dbname=chat","root","");
// abra ke dabra... boom
}
回答by Mark Baker
The $pdo
object isn't in scope within your function.
该$pdo
对象不在您的函数范围内。
回答by Humphrey
@Anvd . I had the same problem but I did solve it by connecting the database in the same page not just to include the coonnecting page . It worked for me
@Anvd 。我遇到了同样的问题,但我确实通过在同一页面中连接数据库来解决它,而不仅仅是包含连接页面。它对我有用
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf-8','root','');
} catch(PDOException $e){
echo 'Connection failed'.$e->getMessage();
}
?>
回答by sebastian
In regards the equivalent of mysql_num_rows in PDO is basically FETCH_NUM it return a index number of the selected row.
关于 mysql_num_rows 在 PDO 中的等价物基本上是 FETCH_NUM 它返回所选行的索引号。
回答by no92
I was getting the same error: Then I saw I called my class after I had closed the PDO connection.
我遇到了同样的错误:然后我看到我在关闭 PDO 连接后给我的班级打电话。
回答by Sanner
Yes, I also learned this the hard way, you need to open DB connection inside the function. I assumed the connection to the DB would be opened inside the function if I opened before calling the function, but no. So:
是的,我也很艰难地学到了这一点,您需要在函数内部打开数据库连接。如果我在调用函数之前打开,我假设与数据库的连接将在函数内部打开,但没有。所以:
function whatever(){
//OPEN DB CONNECTION
CODE
//CLOSE DB
return whateverValue;
}
回答by Jordan
You may also get this error from active, unbuffered queries still active.
您也可能从仍处于活动状态的活动的、未缓冲的查询中得到此错误。
so, on line 41,
所以,在第 41 行,
$stmt = null;
回答by yogesh chatrola
try this code
试试这个代码
$query =$pdo->prepare("SELECT email FROM users WHERE email = ?");
$email = mysql_real_escape_string($_POST['email']);
$stmt->bindValue(1, $email);
$ok = $stmt->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
if ($results == 0) {
return true;
} else {
echo '<h1>something</h1>';
return false;
}