2015-12-01 6 views
5

私は、読みやすさのためだけでなく、私が取り組んでいるプロジェクトのカスタマイズのために私のコードを短縮するというトリックをやろうとしています。クラス内の関数としてのデータベーステーブルの作成

私はDataBaseに接続するクラスを作成しましたが、使用する関数に苦労しているので、列を含むテーブルが作成されます。

クラスは次のようになり、これまでに:あなたが見ることができるように

class DataBase { 

    private $link; 
    private $host, $username, $password, $database; 

    public function __construct($host, $username, $password, $database){ 
     $this->host  = $host; 
     $this->username = $username; 
     $this->password = $password; 
     $this->database = $database; 

     $this->link = mysql_connect($this->host, $this->username, $this->password) 
      OR die("There was a problem connecting to the database."); 

     mysql_select_db($this->database, $this->link) 
      OR die("There was a problem selecting the database."); 

     return true; 
    } 

    public function query($query) { 
     $result = mysql_query($query); 
     if (!$result) die('Invalid query: ' . mysql_error()); 
     return $result; 
    } 

    public function __destruct() { 
     mysql_close($this->link) 
      OR die("There was a problem disconnecting from the database."); 
    } 
} 

クエリの方法は、既に追加されています。その実行がある方法の例は:

$db = new DataBase('localhost',$user,$pass,$name); 
$db->query('SELECT * FROM table WHERE id="0"'); 

可能誰も私の挿入テーブルを追加するための機能を追加するためのいくつかのコードを送ってもらえますか?私はこれを試してみました:

public function create_table($t_data) { 
    $result = $t_data; 
    if (!$result) die('Invalid query: ' . mysql_error()); 
    return $result; 
} 

使用法:私はあなたがそれが今立っているように脆弱である非推奨の機能mysqlを使用しているので、MySQLiまたはPDOを見て推薦する

$t_data = 'CREATE TABLE log_users(
    uid VARCHAR(1024) NOT NULL, 
    username VARCHAR(33) NOT NULL, 
    password VARCHAR(18) NOT NULL, 
    admin VARCHAR(1) DEFAULT 0, 
    key VARCHAR(18) NOT NULL, 
    constant VARCHAR(1) DEFAULT 0)'; 

$db->create_table($t_data); 
+0

あなたの現在の 'query'機能はSQLインジェクション攻撃の影響を非常に受けやすく、攻撃者が簡単にデータベースを破壊したり侵害したりする可能性があります。何年も使われていない 'mysql'ライブラリも使用しています。少なくともmysqliやそれより優れた' PDO'を使うべきです。 PDOを使用すると頭痛が軽減され、SQLインジェクション攻撃を処理するDBクラスの基本フレームワークをここに記述しています。https://github.com/alexmk92/ASFramework/blob/master/app/core/models/Database.php – Alex

+0

@Alexありがとうございました!それは実際に私が必要としたものです!私はこのためのソースコード内のリンクを保持します! –

+0

質問、どうすればいいですか?私はSQLiに慣れていない。それはでしょうか: $ db = new Database(); $ db-> fetch( 'SELECT FROM * table'); &これらでテーブルを作成するにはどうすればよいですか?また、ありがとう! @Alex –

答えて

1

。あなたのクラスを更新しました。これはまた、テーブルを作成できないというオリジナルの問題を修正します。

class DataBase { 

    private $link; 
    // May not need these, see updated __construct method 
    private $host, $username, $password, $database; 

    public function __construct($host, $username, $password, $database){ 
     // Unless you need them elsewhere, no reason to set $this->host, $this->username, etc...you can just access directly like below 
     $this->link = new mysqli($host, $username, $password, $database); 

     // Check connection (which also checks selection of database) 
     if ($this->link->connect_error) { 
      die("Connection failed: " . $this->link->connect_error); 
     } 
    } 

    // You will need to research and update this to work with mysqli (right now it's ripe for SQL injection)! 
    public function query($query) { 
     $result = mysql_query($query); 
     if (!$result) die('Invalid query: ' . mysql_error()); 
     return $result; 
    } 

    // This method will create a table based on the SQL you send it 
    public function create_table($sql) { 
     if ($this->link->query($sql) === TRUE) { 
      return "Table created successfully"; 
     } else { 
      return "Error creating table: " . $this->link->error; 
     } 
    } 

    // Close connection 
    public function __destruct() { 
     $this->link->close(); 
    } 
} 
関連する問題