BorysSokolov Posted June 7, 2013 Share Posted June 7, 2013 I'm working through an example in the book Pro PHP and jQuery by Jason Lengstorf, and keep receiving the error Notice: Undefined variable: db in ...\sys\class\class.db_connect.inc.php on line 30 I do know why I am receiving the error, but I am not sure how to go about solving it. I'm not exactly sure what the author meant to do with the variable db. Here's the code: class DB_Connect { /** * Stores a database object * * @var object A database object */ protected $db; /** * Checks for a DB object or creates one if one isn't found * * @param object $dbo A database object */ protected function __construct($dbo=NULL)//assigns $dbo a default value { if(is_object($db)){ $this->db = $db; }else{ //Constants are defined in /sys/config/db-cred.inc.php $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME; try{ $this->db = new PDO($dsn, DB_USER, DB_PASS); }catch(Exception $e){ //If the DB connection fails, output the error die($e->getMessage()); } } } } Nowhere in the code is the variable ever assigned a value, or even declared, prior to making the check, so I'm not even sure what's it supposed to do. I double-checked that I haven't missed any code, but surely I must have omitted something... Can anyone figure it out? Quote Link to comment https://forums.phpfreaks.com/topic/278873-working-through-a-book-undefined-variable/ Share on other sites More sharing options...
Solution Jessica Posted June 7, 2013 Solution Share Posted June 7, 2013 The function parameter is $dbo. Then you look for $db. Quote Link to comment https://forums.phpfreaks.com/topic/278873-working-through-a-book-undefined-variable/#findComment-1434577 Share on other sites More sharing options...
BorysSokolov Posted June 7, 2013 Author Share Posted June 7, 2013 (edited) I changed the parameter from $dbo to $db, and now everything works fine. I suppose I should have reviewed the code before posting. Edited June 7, 2013 by BorysSokolov Quote Link to comment https://forums.phpfreaks.com/topic/278873-working-through-a-book-undefined-variable/#findComment-1434588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.