php 使用准备好的语句时“还不允许访问属性”警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18831863/
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
"Property access is not allowed yet" warning when using prepared statement
提问by weeix
I'm trying to make a log in system by using AES_ENCRYPT()
to encode my password. But I have some warning from xdebugwhen trying to implement these codes:
我正在尝试通过使用AES_ENCRYPT()
对我的密码进行编码来创建登录系统。但是在尝试实现这些代码时,我收到了来自xdebug 的一些警告:
...
$key = 'd0gis=SUPER-cute';
$sql = "SELECT * FROM `users2` WHERE username = ? AND pwd = AES_ENCRYPT(?, ?)";
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('sss', $username, $password, $key);
$stmt->execute();
$stmt->store_result();
...
When the debugger meets line 8 or $stmt->prepare($sql);
, 6 same warning tables from xdebugsays:
当调试器遇到第 8 行或第 8 行时$stmt->prepare($sql);
,来自xdebug 的6 个相同警告表显示:
(!) Warning: main(): Property access is not allowed yet in D:\xampp\htdocs\learnphp\includes\authenticate_mysqli.inc.php on line 8
(!) 警告:main():第 8 行的 D:\xampp\htdocs\learnphp\includes\authenticate_mysqli.inc.php 中还不允许属性访问
The error property in $stmt
is empty and I have no real problem, but I just want to know what cause this warning messages to appear.
中的错误属性$stmt
为空,我没有真正的问题,但我只想知道是什么原因导致出现此警告消息。
Googled this warning message but didn't find any solution:
谷歌搜索此警告消息,但没有找到任何解决方案:
回答by Markus Malkusch
Your mysql connection was probably not established. After mysqli::__construct()
you have to check mysqli::$connect_error
, which was broken for some PHP versions:
您的 mysql 连接可能未建立。在mysqli::__construct()
您必须检查之后mysqli::$connect_error
,某些 PHP 版本已损坏:
The mysqli->connect_error property only works properly as of PHP versions 5.2.9 and 5.3.0. Use the mysqli_connect_error() function if compatibility with earlier PHP versions is required.
mysqli->connect_error 属性仅在 PHP 5.2.9 和 5.3.0 版中正常工作。如果需要与早期 PHP 版本兼容,请使用 mysqli_connect_error() 函数。
See the connection boiler plate from the documentation of mysqli::__construct()
:
请参阅以下文档中的连接样板mysqli::__construct()
:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
/*
* Use this instead of $connect_error if you need to ensure
* compatibility with PHP versions prior to 5.2.9 and 5.3.0.
*/
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
回答by Evan de la Cruz
EDIT: I believe the issue I describe below is the cause of the issue the OP describes, however since the problem I describe in my answer does not produce the exact same error message, I am no longer sure this is the best answer.
编辑:我相信我在下面描述的问题是 OP 描述的问题的原因,但是由于我在答案中描述的问题不会产生完全相同的错误消息,我不再确定这是最佳答案。
Also, I noticed this from the PHP docs comment section:
另外,我从 PHP 文档评论部分注意到了这一点:
This parameter (and presumably any other parameter in mysqli_stmt) will raise an error with the message "Property access is not allowed yet" if the statement was not prepared properly, or not prepared at all.
To prevent this, always ensure that the return value of the "prepare" statement is true before accessing these properties.
如果语句没有准备好,或者根本没有准备好,这个参数(可能还有 mysqli_stmt 中的任何其他参数)将引发错误消息“还不允许访问属性”。
为防止这种情况,在访问这些属性之前,请始终确保“prepare”语句的返回值为真。
Original answer: This warning occurs when you try to evaluate certain objects (an instance of a class) as a string.
原始答案:当您尝试将某些对象(类的实例)作为字符串求值时,会出现此警告。
Your debugger/IDE is trying to evaluate one of your variables ($stmt), maybe in a watch list or call stack, and it cannot be evaluated as a string.
您的调试器/IDE 正在尝试评估您的变量之一 ($stmt),可能在监视列表或调用堆栈中,并且无法将其评估为字符串。
If you do print_r
on the variable you will get the same error, because PHP cannot turn it into a string.
如果你print_r
在变量上做你会得到同样的错误,因为 PHP 不能把它变成一个字符串。
In your case, it is the $stmt that PHP cannot turn into a string.
在您的情况下,PHP 无法将 $stmt 转换为字符串。
Put this code on line 7 and you will see the error there:
将此代码放在第 7 行,您将在那里看到错误:
print_r($stmt);
Somewhat side note: I never had this issue before recently. Lately I've been getting it a lot. Why doesn't php just skip the inaccessible properties and print the rest? I believe it has to do with the scope of the properties or the use of getters/setters but I am not quite sure yet. I will update when I figure that part out.
有点旁注:我最近从未遇到过这个问题。最近我得到了很多。为什么 php 不跳过无法访问的属性并打印其余的?我相信这与属性的范围或 getter/setter 的使用有关,但我还不太确定。当我弄清楚那部分时,我会更新。
from the official PHP documentation:(http://php.net/manual/en/language.oop5.magic.php#object.tostring)
从官方的PHP文档:(http://php.net/manual/en/language.oop5.magic.php#object.tostring)
The __toString() method allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print. This method must return a string, as otherwise a fatal E_RECOVERABLE_ERROR level error is emitted...
...It is worth noting that before PHP 5.2.0 the __toString() method was only called when it was directly combined with echo or print. Since PHP 5.2.0, it is called in any string context (e.g. in printf() with %s modifier) but not in other types contexts (e.g. with %d modifier). Since PHP 5.2.0, converting objects without __toString() method to string would cause E_RECOVERABLE_ERROR.
__toString() 方法允许类决定当它被视为字符串时将如何反应。比如什么 echo $obj; 将打印。此方法必须返回一个字符串,否则会发出致命的 E_RECOVERABLE_ERROR 级别错误...
...值得注意的是,在 PHP 5.2.0 之前,__toString() 方法仅在与 echo 或 print 直接结合时才被调用。自 PHP 5.2.0 起,它可以在任何字符串上下文中调用(例如在 printf() 中使用 %s 修饰符),但不能在其他类型上下文中调用(例如使用 %d 修饰符)。自 PHP 5.2.0 起,将没有 __toString() 方法的对象转换为字符串会导致 E_RECOVERABLE_ERROR。
回答by BrianHenryIE
I got Property access is not allowed yetwhen running tests inside PhpStorm. I had the project's PHP Lanugage Levelset to 7.2 and the CLI Interpreterset to 7.1. I changed the interpreter to 7.4 and the error went away.
在 PhpStorm 中运行测试时,我还不允许访问属性。我将项目的PHP 语言级别设置为 7.2,并将CLI 解释器设置为 7.1。我将解释器更改为 7.4,错误消失了。