开发者

Reason for: Trying to get property of non-object in php

开发者_如何转开发$EODQuery = "SELECT * FROM EOD WHERE Symbol LIKE '$start' LIMIT 1 OFFSET $limit";

$EODRes = $mysqli->query($EODQuery);

I get an error, but not when i directly query the database.

  <?php $i = 0; while($i <= 4) { $EODRow = getEOD("A%",$i); $i++; echo $EODRow;  } ?>


Your error message has nothing to do with the query itself. What's happening is this: You're calling a method query() on the $mysqli object - however, PHP thinks that it's not actually an object.

The reason for this can be:

  • You forgot to create this object (See http://www.php.net/manual/en/mysqli.connect.php). Here's an example:

    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
    
  • You mistyped the variable name, so your mysqli object is actually called something else.

  • The code you show is within a function or a method, but your $mysqli object is in the global scope. In this case, you should "load" it into the function scope, like this:

    function doStuff() {
        global $mysqli;
    
        $mysqli->query(...);
    }
    

    For more information regarding variable scope, see this link: http://php.net/manual/en/language.variables.scope.php


On the first look it seems like you have forgotten to instantiate $mysqli, that leads to $mysqli not being an object, so you cant execute the query() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜