Best instance PDO class for rapidly creating small project.
Edit the file in Db.php
with real data, for example :
protected static $_instance = null;
protected static $username = "root";
protected static $password = "";
protected static $dsn = "mysql:host=localhost;dbname=test";
This class have multiple method like "find, where, limit, orderBy, statement, one, all, save, delete, deleteAll". Please see the example using this class. Please ensure you have include 'Query.php'
This following code will get single row using one() method.
$query = new Query;
$query->find("my_table")
->where(['status' => 1])
->orderBy("id desc");
//print result single row using one()
$one = $query->one();
echo "<pre>";print_r($one);
This following code will get many row using all
method.
$query = new Query;
$query->find("user")
->where(['status' => 1])
->limit(2)
->orderBy("id desc");
$result = $query->all();
//print result
echo "<pre>";print_r($result);
This following code will get many row using all() method.
$qr = new Query();
$models = $qr->find("user")
->where(['status'=>1])
->andFilterWhere(['LIKE', 'name', '%Andy%'])
->all();
if ($models)
foreach ($models as $model) {
echo 'name is :' . $model->name . ' and username is ' . $model->username . ' <p>';
}
// Search AND IN array
$qr = new Query();
$models = $qr->find("user")
->where(['status' => 1])
->andFilterWhere(['IN', 'id', [1, 2, 3, 4, 5, 6, 7, 9, 10, 11]])
->all()
;
// Search OR IN array
$qr = new Query();
$models = $qr->find("user")
->where(['status' => 1])
//->andFilterWhere(['IN', 'id', [1, 2, 3, 4, 5, 6, 7, 9, 10, 11]])
->orFilterWhere(['LIKE', 'name', '%Andy%'])
->all()
;
This following example how to insert data
$query = new Query;
$query->find('user');
$query->name = "Andy Laser";
$query->email = '[email protected]';
$query->username = "laser";
$query->save();
echo "<pre>";print_r($query);
echo 'my id is:' . $query->id . ' and my name is ' . $query->name. ' and table name is ' . $query->table;
This following example how to update data and using where and one() method
$user = new Query();
$user->find('user')->where(['id' => 12])->one();
$user->username = 'testing1123232';
$user->save();
echo 'my id is:' . $query->id . ' and my name is ' . $query->name . ' and table name is ' . $query->table;
This following example how to delete using delete() or deleteAll(). Callback value 1 for success or 0 not success
//delete one
//return 1 or 0
$delete = new Query();
$delete->find("user")->where(['status' => 1])->delete();
// or you can do like this following
$users = new Query();
$users->find('user')->where(['username' => 'laser])->one();
$users->delete();
//delete all
//return 1 or 0
$delete = new Query();
$delete->find("user")->where(['status' => 1])->deleteAll();
// or you can do like this following
$users = new Query();
$users->find('user')->where(['username' => 'laser])->all();
$users->deleteAll();
Looping example,
// looping
if ($result)
foreach ($result as $res) {
echo ' id :' . $res->id . ' username is :' . $res->username;
}