2016-10-01 20 views
1

からデータをretriveすることはできません。私はデータベースからデータを取得しようとしていますが、私的なものに関連するものは私を立ち往生させます。 これは私のコードです。OOP-PHP - 私はPHPでOOPモデルと新たなんだデータベース

<?php 

require ("UserData.php"); 

class Database{ 

    public function getUser($sql){ 

    include ("includes/connect.php"); 
     $statement = $conn->prepare ($sql); 
     $statement->execute(); 

     while ($row = $statement->fetch()) { 

      $dataSet[] = new UserData($row); 
     } 

     if (!empty($dataSet)) { 
      return $dataSet; 

     }else{ 
      die(); 
     } 
    } 
} 
?> 

第二のファイル

<?php 

class UserData 
{ 
    private $user_id, $phone,$name,$address; 

    public function _construct($dbrow){ 

     $this->user_id = $dbrow['user_id']; 
     $this->name = $dbrow['name']; 
     $this->phone = $dbrow['phone']; 
     $this->address = $dbrow['address']; 
    } 
    public function getUserId(){ 
     return $this->user_id; 
    } 
    public function getUserName(){ 
     return $this->name; 
    } 
    public function getUserPhone(){ 
     return $this->phone; 
    } 
    public function getUserAddress(){ 
     return $this->address; 
    } 
} 
?> 

<?php require ("Database.php"); ?> 

<html> 
<head> 
    <title>OOP</title> 
</head> 
<body> 
    <?php 
    include("includes/connect.php"); 

    $db = new Database(); 

    $dataSet = $db -> getUser ("SELECT * from user"); 


    if ($dataSet) { 

     foreach ($dataSet as $data) { 
      echo "<p>"; 
      echo "ID" .$data->getUserId()."<br />"; 
      echo "Name" .$data->getUserName()."<br />"; 
      echo "Phone" .$data->getUserPhone()."<br />"; 
      echo "Address" .$data->getUserAddress()."<br />"; 
      echo "</p>"; 
     } 

    }else{ 
     echo "no result found"; 
    } 
    ?> 
</body> 
</html> 

最後の1まあ、私はvar_dumpdataSetにしようとしたが、エラーが表示されます。

アレイ(2){[0] =>オブジェクト(UserDataの)#5(4){ [ "USER_ID": "UserDataの":プライベート] => NULL [ "電話": "UserDataを":プライベート] => NULL [ "名前": "UserDataの":プライベート] => NULL [ "アドレス": "UserDataの":プライベート] => NULL} [1] =>オブジェクト(UserDataの)#6(4) { [ "USER_ID": "UserDataの":プライベート] => NULL [ "電話": "UserDataの":プライベート] => NULL [ "名前": "UserDataの":プライベート] => NULL [ "アドレス" : "UserData":private} => NULL}}

誰にでもどのスポットがコードダンプを作成するのですか?

答えて

3

すべてがUserDataクラスの

public function _construct($dbrow){ ... 
       ^^^^^^^^^^ it should be double underscore, not single 

あなたのコンストラクタメソッドが間違っている、この行を除いて大丈夫です。それは、あるべき

public function __construct($dbrow){ ... 
+0

いまいましい、uが私の夜の男を保存!!!!私は許可されたときにティックをクリックします!私は助けることがうれしい – vinataba

+0

@vinataba。 *乾杯! : - )* –

0

PHP 5.6.0+では、あなたは魔法を使うことができますが、あなたがgetUserId

のようなメソッドを持っているかにメソッドを作成するときに意図していない

class UserData 
{ 
    public function __debugInfo() { 
     return ['user_id' => $this->user_id, 'phone' => $this->phone, 'name' => $this->name, 'address' => $this->address]; 
    } 
} 

かなりの変数公共__debugInfoを()、mathodダンプのような:

クラスところで
class UserData 
{ 
    public function dump() { 
     var_dump($this); 
    } 
} 

あなたはメソッドが呼び出されたすべての時間を含めるgetUserDatabase方法、簡単な修正:require_onceの代わりincludeを使用し、より良いクラスのinclude/require/require_once外を呼び出すようになります

関連する問題