Results 1 to 1 of 1

Thread: PDO with classes

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default PDO with classes

    So i am trying to make connect to a database. However, I am running into the issue of grabbing strings to connect to the database. First one fails, second one is successful.

    PHP Code:
    class dbedits {
        private 
    $pdo null;
        private 
    $servername "localhost";
        private 
    $dbusername "root";
        private 
    $dbpassword "";

        public function 
    dbconnect($dbname){

            try{
                
    $this->$pdo = new PDO("mysql:host=".$this->servername$this->$dbusername$this->$dbpassword);
                
    $this->$pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
                
    $dbname "`".str_replace("`","``",$dbname)."`";
                
    $this->$pdo->query("CREATE DATABASE IF NOT EXISTS $dbname");
                
    $this->$pdo->query("use $dbname");
            }catch(
    PDOException $e){
                echo 
    $e->getMessage();
            }
        }

        public function 
    dbcreate($dbname){            
            
    //$this->dbconnect($dbname);
            
    try{
                
    $pdo = new PDO("mysql:host=".$this->servername"root","");
                
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
                
    $dbname "`".str_replace("`","``",$dbname)."`";
                
    $pdo->query("CREATE DATABASE IF NOT EXISTS $dbname");
                
    $pdo->query("use $dbname");
            }catch(
    PDOException $e){
                echo 
    $e->getMessage();
            }
        }

    I fixed it. I forgot to remove the '$' top ones.

    PHP Code:
    public function dbconnect($dbname){ 

            try{ 
                
    $this->pdo = new PDO("mysql:host=".$this->servername$this->dbusername$this->dbpassword); 
                
    $this->pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
                
    $dbname "`".str_replace("`","``",$dbname)."`"
                
    $this->pdo->query("CREATE DATABASE IF NOT EXISTS $dbname"); 
                
    $this->pdo->query("use $dbname"); 
            }catch(
    PDOException $e){ 
                echo 
    $e->getMessage(); 
            } 
        } 
    Last edited by Deadweight; 02-17-2016 at 06:35 PM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Two Classes in one
    By dude9er in forum CSS
    Replies: 2
    Last Post: 05-07-2008, 10:43 PM
  2. JS Classes?
    By ??? in forum JavaScript
    Replies: 3
    Last Post: 06-17-2007, 10:53 PM
  3. PHP 4+ Classes
    By Acey99 in forum PHP
    Replies: 1
    Last Post: 12-16-2006, 12:07 AM
  4. different classes for li
    By lorrens in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 10-16-2006, 05:16 PM
  5. Classes v ID
    By AlistairH in forum CSS
    Replies: 3
    Last Post: 06-29-2005, 09:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •