Database layer class I'm trying to get familiar with database handling.  Can you point out my errors and what I should change?

I want to learn new methods, but just don't want to learn it the wrong way.

I do realize these things below, yet I wanted to make the code more clear:


I need to bind values for insert()
Use try/catch for error handling
Password hashing (did not use it to make example simpler)




&lt;?php

class Query 
{
    private $_sql;
    private $_sth;
    private $_db;

    public function __construct() 
    {
        $this-&gt;_db = new PDO('mysql:host=localhost;dbname=mvc;', 'root', '');
        $this-&gt;_db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this-&gt;_db-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    }

    public function select($arg)
    {
        $this-&gt;_sql .= "SELECT {$arg}";
        return $this;
    }

    public function from($arg)
    {
        $this-&gt;_sql .= " FROM {$arg}";
        return $this;
    } 

    public function insert($arg)
    {
        $this-&gt;_sql .= "INSERT INTO {$arg}";
        return $this;        
    }

    public function columns($arg)
    {
        $this-&gt;_sql .= " ({$arg})";
        return $this;            
    }

    public function values($arg)
    {
        $this-&gt;_sql .= " VALUES ({$arg})";
        return $this;            
    }

    public function execute($data = null)
    {
        $this-&gt;_sth = $this-&gt;_db-&gt;prepare($this-&gt;_sql);
        $this-&gt;_sth-&gt;execute($data);
        $this-&gt;_sql = null;
        return $this;
    }

    public function fetch()
    {
        return $this-&gt;_sth-&gt;fetchAll();
    }

    public function getSql()
    {
        return $this-&gt;_sql;
    }
}


$query = new Query;

// inserts into database
$query-&gt;insert('users')
        -&gt;columns('`username`,`password`')
        -&gt;values('"test","tester"')
        -&gt;execute();

// returns array of users
$query-&gt;select('username')
        -&gt;from('users')
        -&gt;execute()
        -&gt;fetch()

?&gt;
Passwords as plain text!?
If I were using an application or website that stored my password as plain text where anyone with access to the DB seeing this would make me feel very scared:
// inserts into database
$query-&gt;insert('users')
        -&gt;columns('`username`,`password`')
        -&gt;values('&quot;test&quot;,&quot;tester&quot;')
        -&gt;execute();

For the love of your users, please consider password hashing!

Password hashing is one of the most basic security considerations that must be made when designing any application that accepts passwords from users. Without hashing, any passwords that are stored in your application's database can be stolen if the database is compromised, and then immediately used to compromise not only your application, but also the accounts of your users on other services, if they do not use unique passwords.

Redundancy
This:
public function insert($arg)
{
    $this-&gt;_sql .= &quot;INSERT INTO {$arg}&quot;;
    return $this;        
}

public function columns($arg)
{
    $this-&gt;_sql .= &quot; ({$arg})&quot;;
    return $this;            
}

public function values($arg)
{
    $this-&gt;_sql .= &quot; VALUES ({$arg})&quot;;
    return $this;            
}

And this:
// inserts into database
$query-&gt;insert('users')
        -&gt;columns('`username`,`password`')
        -&gt;values('&quot;test&quot;,&quot;tester&quot;')
        -&gt;execute();

That seems redundant, unless you plan on inserting user names and passwords in different tables/columns. Why not shorten it like this:
public function insert($arg)
{
    $this-&gt;_sql .= &quot;INSERT INTO users (`username`, `password`) VALUES ({$arg})&quot;;
    return $this;        
}