13

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
Community
  • 1
  • 1
weeix
  • 1,359
  • 1
  • 14
  • 17
  • 1
    Why are you using `stmt_init()`? I've never seen that used before. Usually it's `$db->prepare(...)` directly. – tadman Sep 16 '13 at 16:01
  • I follow an [example](http://www.php.net/manual/en/mysqli-stmt.prepare.php#example-1788) in php.net – weeix Sep 17 '13 at 02:52
  • 1
    That's odd because there's [other documentation](http://www.php.net/manual/en/mysqli.prepare.php) that suggests you can call `prepare` directly on the link you have, and I've always seen it done that way. – tadman Sep 17 '13 at 15:22
  • Please see my answer for this here: http://stackoverflow.com/questions/28870315/mysqli-property-access-is-not-allowed-yet/33469517#33469517 (dupe question, not sure how to flag it as such though) – QuestionOverflow Nov 10 '15 at 12:56
  • 1
    maybe this: http://stackoverflow.com/questions/25377030/mysqli-xdebug-breakpoint-after-closing-statment-result-in-many-warnings – troseman Feb 25 '16 at 20:00
  • The second link in your question claims to address this exact issue: "To prevent this, always ensure that the return value of the prepare statement is true before accessing these properties." http://php.net/manual/en/mysqli-stmt.param-count.php#89490 – Evan de la Cruz Mar 18 '16 at 00:16

3 Answers3

3

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());
}
Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67
1

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.

Evan de la Cruz
  • 1,966
  • 1
  • 13
  • 17
  • 1
    This doesn't seem the be case. It's a bug with PHP and debugger; see my "duplicate" flag. – Pere Mar 09 '16 at 10:53
  • I don't see your duplicate flag. However I will confirm that this absolutely is the case in some environments. The cause of this issue, at least in the case that I found, tested, and verified, had to do with trying to evaluate an object (instance of a class) as a string. It is not a bug because PHP has it clearly documented: http://php.net/manual/en/language.oop5.magic.php#object.tostring – Evan de la Cruz Mar 18 '16 at 00:00
  • The docs also state that this behavior changed starting with PHP 5.2. I'll update my answer to include the snippet from http://php.net/manual/en/language.oop5.magic.php – Evan de la Cruz Mar 18 '16 at 00:01
  • Actually I want to backtrack a little bit. While I believe these issues are coupled/directly related, it appears that the warning I am describing is not exactly the same as the OP. I also found this comment on the PHP docs that describes the OPs exact issue: http://php.net/manual/en/mysqli-stmt.param-count.php#89490 – Evan de la Cruz Mar 18 '16 at 00:12
0

I got Property access is not allowed yet when running tests inside PhpStorm. I had the project's PHP Lanugage Level set to 7.2 and the CLI Interpreter set to 7.1. I changed the interpreter to 7.4 and the error went away.

BrianHenryIE
  • 520
  • 5
  • 12