2015-01-15 154 views
-1

dbConnect.php數據庫連接類頁這個PDO OOP方法是否正確?

class dbConnect { 

    /* 
    * initiate mysql database host,username,password,database name 
    */ 

    private $host; 
    private $dbName; 
    private $uname; 
    private $upass; 
    protected $con; 

    public function __construct($host, $database, $userName, $password) { 
     $this->host = $host; 
     $this->dbName = $database; 
     $this->uname = $userName; 
     $this->upass = $password; 
     $this->connectDB(); 
    } 

    public function connectDB() { 
     /* 
     * @var $dsn mean data source name for pdo connection 
     */ 
     $dsn = "mysql:host=" . $this->host . ";port=3306;charset=utf8;dbname=" . $this->dbName; 
     try { 
      $this->con = new PDO($dsn, $this->uname, $this->upass); 
      $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      $this->con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
      return $this->con;    
     } catch (Exception $e) { 
      echo $e->getMessage(); 
     } 
    } 
} 

sampleView.php sampleViews類頁

/* 
* include dbConnect class for this SampleView Class Page 
*/ 
require_once 'class/dbConnect.php'; 

class sampleViews extends dbConnect { 

    public function __construct() { 
     parent::__construct('localhost', 'test2', 'root', ''); 
    } 

    function chkConnection() { 
     /* 
     * dbConncect Class Connection variable access 
     */ 
     $dbcon = $this->con; 
     if ($dbcon) { 
      echo "successfully Connect database"; 
     } else { 
      echo "sorry Could not be connect databsae"; 
     } 
    } 

    /* 
    * In CRUD method implementation 
    * READ Method Sample 
    */ 

    function getData() { 
     $sql = "SELECT " 
       . "users.uid, " 
       . "users.uname, " 
       . "users.upass " 
       . "FROM " 
       . "users"; 
     try { 
      $stmt = $this->con->prepare($sql); 
      $stmt->execute(); 
      while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
       $array[] = $row; 
      } 
      return $array; 
     } catch (Exception $exc) { 
      echo $exc->getMessage(); 
     } 
    } 

    /* 
    * CREATE Method 
    */ 

    function createSample() { 
     /* 
     * ini variable for insert 
     */ 
     $uname = "රුවන්"; 
     $upass = "123"; 

     //Sql insert query 
     $sql = "INSERT INTO `test2`.`users` (`uname`, `upass`) VALUES (:uname, :upass);"; 

     try { 

      /* 
      * prepare Insert Query 
      */ 
      $createStmt = $this->con->prepare($sql); 
      /* 
      * bindParam method use for inset user data preventing sql injection 
      */ 
      $createStmt->bindParam(":uname", $uname, PDO::PARAM_STR, 150); 
      $createStmt->bindParam(":upass", $upass, PDO::PARAM_STR, 150); 
      /* 
      * execute insert query 
      */ 
      if ($createStmt->execute()) { 
       echo "successfully Inserted"; 
      } else { 
       echo "sorry could not be inserted"; 
      } 
     } catch (Exception $exc) { 
      echo $exc->getMessages(); 
     } 
    } 

} 

我的問題是在第二類爲樣本的意見。 Parent Class $con變量可以訪問但是Net Bean IDE不顯示任何建議。準備,執行,綁定Param或與PDO數據庫連接相關的任何事物。所以我認爲我的OOP方法是錯誤的或不正確的。任何人都可以確認我是否正確?

+0

這個問題屬於對堆棧交換網絡中的其他網站:http://codereview.stackexchange.com/ – sectus

回答

0

PHP不強類型 - 方法簽名不提供有關返回類型的信息。變量沒有關於類型的信息。爲了幫助你的IDE,你必須使用描述類型的PHPDoc註釋。

添加到$ CON財產評論:

class dbConnect { 

    private $host; 
    private $dbName; 
    private $uname; 
    private $upass; 
    /** 
    * @var PDO 
    */ 
    protected $con; 
+0

非常感謝你老兄.... ..........這是正確的。 – user1510741