last day (15 days later) » 

1:22 PM
0
Q: Undefined variables in php on including config files

cRAN <?php class Login { private $dbh = null; private $dsn ,$host, $username , $password= null; public $errors = array(); public $messages = array(); public function __construct() { session_start(); require_once 'dbconfig.php'; if (isset($_GET["lo...

 
Class properties are accessed using $this->host, not simply $host (the latter is used for local scope variables)
 
So should I include $this->$host in the query itself @MarkBaker
Now I am stuck with this : Catchable fatal error: Object of class Login could not be converted to string. :/ @MarkBaker
 
$this->$dsn = "mysql:host={$this->host};dbname=$dbname";... though I can't see anwhere you're defining $dbname anywhere in the scope of the class
$this->$dbh = new PDO($this->dsn, $this->username, $this->password);
And as you're using PDO, learn to use prepared statements with bind variables
 
that's defined in the config file i had included "require_once dbconfig.php"
 
Simply defining a variable somewhere in a script doesn't make it accessible from everywhere in your script.... variables have scope
 
1:29 PM
I am not able to resolve Catchable fatal error: Object of class Login could not be converted to string. please help!!
 
That fatal error has nothing to do with fixing the faults in the class code itself
There's a lot of major faults in that class, not least the references to $host when it should be $this->host, etc
 
That error (except the object one) was solved . And I am learning to use PDO and your suggestion helped me a lot. Thanks :)
 
Catchable fatal error: Object of class Login could not be converted to string isn't being thrown by anything in the code that you've posted, but by the way you access the class from elsewhere in your code
 
yes there's a index.php file
 
I'm guessing that somewhere you're trying to echo your instance of the Login class somewhere in your index.php
 
1:34 PM
$login = new Login();

if ($login->isUserLoggedIn() == true) {
include 'logged_in.php';

} else {
include 'not_logged_in.php';
}
this is what i am trying to do
 
Is anything in not_logged_in.php or logged_in.php trying to display the messages or errors from $login?
Where is Catchable fatal error: Object of class Login could not be converted to string. occurring? You should be able to identify the script/line where that occurs
 
Catchable fatal error: Object of class Login could not be converted to string in C:\wamp\www\proj\Login.php on line 38
and yes I am printing out the messages in the logged_in.php / not_logged_in.php
Should I type cast them?
 
How are you trying to display them?
 
 <?php
 // show potential errors / feedback (from login object)
 if (isset($login)) {
    if ($login->errors) {
    foreach ($login->errors as $error) {
        echo $error;
    }
  }
if ($login->messages) {
    foreach ($login->messages as $message) {
        echo $message;
        }
     }
}
?>
 
You changed $this->$dsn = "mysql:host=$host;dbname=$dbname";.... what exactly did you change it to?
 
1:44 PM
$this->$dsn = "mysql:host=$this->$host;dbname=$this->$dbname"; // $dbname is empdb as in dbconfig.php
$this->$dbh = new PDO($this->$dsn, $this->$username, $this->$password);
$this->$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
Note that I had $this->$host wrapped in { and } when I posted my comment against your question..... it's very important to do so here
You'll need to do the same with $this->$dbname as well
It's to ensure that PHP doesn't misinterpret a possibly ambiguous variable when injecting it into a double-quoted string
Without those curly braces, it's misinterpreting what you're injecting as $this, not as $this->host
 
I had tried it but was getting the same errors :(
$this->$dsn = "mysql:host={$this->$host};dbname={$this->$dbname}";
you mean to say like this
 
$this->$dsn = "mysql:host={$this->host};dbname={$this->dbname}"; not $this->$dsn = "mysql:host={$this->$host};dbname={$this->$dbname}";
When you're using $this->property you don't use a $ in front of the property name, only in front of $this
The only exception to that would be "dynamic properties".... e.g. $property = 'host'; echo $this->$property; where PHP would echo the value of $this->host
 
I am getting this error :
Notice: Undefined property: Login::$dbname in C:\wamp\www\proj\Login.php on line 38
Notice: Undefined variable: dsn in C:\wamp\www\proj\Login.php on line 38
Fatal error: Cannot access empty property in C:\wamp\www\proj\Login.php on line 38
$host = 'localhost';
$dbname = 'empdb';
$username = 'root';
$password = '';
this is the db.config file
 
2:01 PM
Yes, that's your variable scope issue nl1.php.net/manual/en/language.variables.scope.php
If you define variables outside of a class or function, then they don't exist inside that class or function.... unless you specifically put them in that class or function
Modify your Login constructor to accept those values, and set them as properties in the class
 
like $this->$dbname = $dbname;
 
public function __construct($host, $dbname, $username, $password)
{
$this->host = $host;
$this->dbname = $dbname;
... etc
}
Then, when you do $login = new Login(), instead do $login = new Login($host, $dbname, $username, $password);
But not $this->$dbname = $dbname;
Remember not to put a $ before the class property name
 
public function __construct()
{

session_start();
require_once 'dbconfig.php';
if (isset($_GET["logout"])) {
$this->doLogout();
}
elseif (isset($_POST["login"])) {
$this->dbname = $dbname;
$this->password=$password;
$this->host = $host;
$this->dologinWithPostData();
}
}
 
public function __construct() needs the list of variables that you're passing
public function __construct($host, $dbname, $username, $password)
 
thanks sir
that error is removed
but I am still getting an error for $dsn
solved it
but getting an access denied error
 
2:41 PM
I am not able to resolve it :(
please help
 
 
4 hours later…
6:42 PM
Solved it !!!! Thnaks very much @Ma
@MarkBaker . :)
 

last day (15 days later) »