0

I have a db connect function and an abstract database class bause of the way I structure it. I have to keep added my PDO connect codes to the constructor.

My config.php I have

function dbconnect() 
{
            $dbh; // database handler
             $host = 'localhost';
             $user =  'root';
             $pass =  '';
             $dbname = 'test101';
             $error;
                // Set DSN
                $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
                // Set options
                $options = array(
                    PDO::ATTR_PERSISTENT    => true,
                    PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_EMULATE_PREPARES => false

                );
                // Create a new PDO instanace
                try{
                    $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
                }
                // Catch any errors
                catch(PDOException $e){
                    $this->error = $e->getMessage();
                }

           }

in my abstract.class.php

 include_once('config/config.php');

class Database {


           public function __construct() {
               dbconnect(); //connect to database

           }

           public function query($query) {
               $this->stmt = $this->dbh->prepare($query);
           }

}
$database = New Database();

not in config.php I have a dtabase handler $dbh and in abstract.class.php I am calling its in the query function.

My question

I am getting an error Undefined property: Database::$dbh. how can I pass this from my config.php to abstract.class.php? I thought when you using the PHP include this is the same as copying and paste whatever is in that file in the file you want to use it for. How can I pass $dbh from config.php to abstract.class.php please feel free to advice me on anything issues that might happen, or anything with my PDO connection that will do with security issues.

This works if I copy and pass my function in the constructor and set the database connection variables to private $dbh; outside the constructor but the issue with this is the fact of always having to include the class when I dont every need it. sometimes I might just need to connect to the database also I wanted to add define salt to the config.php

infused
  • 22,433
  • 13
  • 63
  • 74
user3057514
  • 251
  • 2
  • 11
  • What do you think `__construct` function defined inside `dbconnect` function would do? – zerkms Feb 26 '14 at 01:05
  • @zerkms sorry forgot to edit that part when I copy the codes over – user3057514 Feb 26 '14 at 01:07
  • the question is still open. They are just 2 nested functions. – zerkms Feb 26 '14 at 01:08
  • what is the purpose of having `__construct` function inside a `dbconnect` function? – zerkms Feb 26 '14 at 01:13
  • Okay, you removed it. Now you're using `$this->` in your function. What it supposed to mean? – zerkms Feb 26 '14 at 01:14
  • So what `$this->` in your function definition means? – zerkms Feb 26 '14 at 01:16
  • as @zerkms said, You are misusing $this, you can only use that when working inside a class: have a look here: http://stackoverflow.com/questions/5494436/php-this-variable – Mazzy Feb 26 '14 at 01:17
  • it is means to point to the function itself or does this only work with functions which are in a class – user3057514 Feb 26 '14 at 01:18
  • 1
    @user3057514: it points to the *current object* – zerkms Feb 26 '14 at 01:18
  • @user3057514 , You can only do that from within classes, just say $dbh = new PDO(...); same for the error – Mazzy Feb 26 '14 at 01:19
  • thanks @Mazzy for the link and @zerkrns. I replace all `$this` with included the onces in $dns` but I am still facing this error `Notice: Undefined variable: dbh` I have also replace remove `this->` in the abstract class. but I am – user3057514 Feb 26 '14 at 01:29
  • 1
    @user3057514 in your database class you are calling $this->dbh, but, dbh doesn't exist in that class, so in your dbconnect function, return $dbh, then assign $this->dbh = dbconnect(); – Mazzy Feb 26 '14 at 01:31
  • @Mazzy thanks a million. I have rate you. If you can but this in an answer i will tick it off thanks again for your time – user3057514 Feb 26 '14 at 01:35
  • @Mazzy in terms of security how secure do you think the PDO connection is? – user3057514 Feb 26 '14 at 01:36
  • @user3057514 The connection will be secure that's not the problem, the security issue will be with SQL injection: http://stackoverflow.com/questions/601300/what-is-sql-injection – Mazzy Feb 26 '14 at 01:37

1 Answers1

1

In your database class you are calling $this->dbh, so in your dbconnect function, return $dbh, then in the database class constructor: $this->dbh = dbconnect();

Mazzy
  • 1,731
  • 2
  • 12
  • 32