Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Property access is not allowed yet" warning when using prepared statement [duplicate]

I'm trying to make a log in system by using AES_ENCRYPT() to encode my password. But I have some warning from xdebug when trying to implement these codes:

...
$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 xdebug says:

(!) Warning: main(): Property access is not allowed yet in D:\xampp\htdocs\learnphp\includes\authenticate_mysqli.inc.php on line 8

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.

Googled this warning message but didn't find any solution:

  1. UPDATE query with prepared statements
  2. http://php.net/manual/en/mysqli-stmt.param-count.php
like image 785
weeix Avatar asked Sep 16 '13 15:09

weeix


2 Answers

Your mysql connection was probably not established. After mysqli::__construct() you have to check mysqli::$connect_error, which was broken for some PHP versions:

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.

See the connection boiler plate from the documentation of 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());
}
like image 121
Markus Malkusch Avatar answered Nov 17 '22 22:11

Markus Malkusch


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.

Also, I noticed this from the PHP docs comment section:

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.

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.

If you do print_r on the variable you will get the same error, because PHP cannot turn it into a string.

In your case, it is the $stmt that PHP cannot turn into a string.

Put this code on line 7 and you will see the error there:

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.

from the official PHP documentation: (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.

like image 38
Evan de la Cruz Avatar answered Nov 17 '22 21:11

Evan de la Cruz