简体   繁体   English

我错过了什么一直给成员函数错误打电话?

[英]What am I missing that keeps giving call to member function error?

I am trying to build a simple query to process search results. 我正在尝试建立一个简单的查询来处理搜索结果。 I will make it PDO safe later, but for now, I keep getting a Call to a member function execute() on a non-object error, BUT I can't see anything below that is causing this. 稍后我将使它的PDO安全,但就目前而言,我一直在发生非对象错误时继续调用成员函数execute(),但看不到任何导致此错误的原因。 The error is at the line $results->execute(); 错误在$ results-> execute();行中。

The PDO object/connection works fine and my code works on every other page but this one. PDO对象/连接工作正常,我的代码在除该页面之外的所有其他页面上均可工作。 What on Earth am I missing? 我到底想念什么? Some syntax error somehow? 某些语法错误以某种方式出现?

$last = $_GET['last'];
$author = $_GET['author'];

$results = $dbh->prepare("select
wp_users.ID AS user_id,
wp_users.ID,
wp_users.display_name,
FROM wp_users
WHERE wp_users.display_name = $author; ");
$results->execute();
$row = $results->fetchAll(PDO::FETCH_ASSOC);

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. 如果数据库服务器成功准备了该语句,则PDO :: prepare()返回PDOStatement对象。 If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling). 如果数据库服务器无法成功准备该语句,则PDO :: prepare()返回FALSE或发出PDOException(取决于错误处理)。

It is returning false, due to invalid sql most likely. 由于最有可能的SQL无效,它返回false。 To check the actual error, you can do: 要检查实际错误,您可以执行以下操作:

if (!$results) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}

Most likely, the variable you are using is not quoted, which is why you should really just use named parameters to begin with, since it's not really any more difficult: 最有可能的是,您使用的变量未加引号,这就是为什么您实际上应该只使用命名参数的原因,因为这实际上并不困难:

 $results = $dbh->prepare("select wp_users.ID AS user_id, 
                           wp_users.ID, 
                           wp_users.display_name,
                           FROM wp_users
                           WHERE wp_users.display_name = :author");

 $results->execute([':author' => $author]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery ajax 调用 php 给出 404 未找到:我错过了什么? - jQuery ajax call to php giving a 404 not found: what am I missing? Cakephp重定向在null上将错误调用给成员函数header() - Cakephp redirect is giving the error Call to a member function header() on null PHP函数-我缺少什么? - PHP Function - what am I missing? 为什么使用Aerys出现“错误:在null上调用成员函数end()”? - Why am I getting “Error: Call to a member function end() on null” using Aerys? 为什么会出现“致命错误:在非对象上调用成员函数fetch_assoc()”? - Why am I getting a “Fatal error: Call to a member function fetch_assoc() on a non-object”? 为什么我会收到“在非对象上调用成员 function”错误? - Why am I getting a “Call to a member function on a non-object” error? 我正在尝试制作注册表单,但是当我运行它时出现此错误致命错误:未捕获错误:在 nul 上调用成员 function query() - I am attempting to make a registration form but when I run it this error appears Fatal error: Uncaught Error: Call to a member function query() on nul 致命错误:调用成员函数,不知道调用变量的内容 - Fatal error: Call to a member function, not sure what to call variable 错误调用成员函数 - error call to member function 函数中的Mysql函数我缺少什么 - Mysql Function within a function what am I missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM