0

I'm trying to build a website using object-oriented functionality, and running into some issues. The one that is currently vexing me is an error message that I can't solve:

Fatal error: Call to a member function real_escape_string() on a non-object in /home/rilbur5/public_html/tutor/login.php on line 41

function get_user_MYSQL(){ return new mysqli($GLOBALS['db_host'],$GLOBALS['user_login'],$GLOBALS['user_password'],$GLOBALS['db_name']); }//will turn this into a singleton object later

class User_Login{
    private $db_link;

    public function __construct()
    {
        $db_link=get_user_MYSQL();
    }

    public function bar($foo)
    {
        $foo=$db_link->real_escape_string($foo);
    }
}

I've inserted an echo statement into the constructor, and it IS being called. I've tested the get_user function, and it works elsewhere -- in fact, I used an echo statement that showed that the data was, in fact, being properly escaped when I used real_escape_string on an object produced by the get_user functions. So why is the combination not producing a mySQLi function that will do the necessary work?

tereško
  • 58,060
  • 25
  • 98
  • 150
RonLugge
  • 5,086
  • 5
  • 33
  • 61
  • Are you checking the return value of `get_user_MYSQL()` to make sure it's returning an object? – AJ. Jun 03 '11 at 02:09
  • While a good point -- I've engaged in unintentional happy path programming here -- it does in fact return a value. A better question would be how do I redesign the class to handle that check... a die function, or return nil and then check in the new statement that it exists? – RonLugge Jun 03 '11 at 02:22

1 Answers1

1

I think you need to use $this->db_link, since you're trying to access the MySQLi object within the class.

  • Well, now I just feel silly -- consider your answer accepted, as soon as the website will let me accept it. – RonLugge Jun 03 '11 at 02:21