Description
GOModel select function executes select query and gets the data from database.
You need to specify tablename from which you want to get the data. You can specify columnames to be retrived.
You can also specify where condition and all different types of operators like =, !=, IN, NOT IN, LIKE,
BETWEEN etc to get data. You can also apply groupby, orderby, having, limit etc. with this function.
By default, data is returned in form of associative array, You can specify different types of format of
data using the fetchtype; It also parses column name and table name to properly use backtics(`) in order to avoid error
due to incorrect way of naming of column/table. If you have used followed naming convention for backtics, then you
can set backtick ="" (empty string).
Function Defination
void function select($dbTableName);
Parameters Details
Details | Type | Example |
---|---|---|
$dbTableName - table name, to perform select query | string | Any valid tablename e.g. "select * from tablename" |
Examples
$GOModel = new GOModel(); //create object of the GOModel class $GOModel->connect("localhost", "root", "", "database");//connect to database /* Select function */ $result = $GOModel->select("emp");
More Examples
//Example 1 - (query generated -> SELECT `empId`,`firstName`,`lastName` FROM `emp` ) //Specify column names $GOModel->columns = array("empId", "firstName", "lastName"); $result = $GOModel->select("emp"); //Example 2 - (query generated -> SELECT * FROM `emp` WHERE `age`>= ? ) //Specify where condition $GOModel->where("age",30,">="); $result = $GOModel->select("emp"); //Example 3 - (query generated -> SELECT * FROM `emp` WHERE `status`= ? AND `age`>= ? AND ( `firstName`= ? OR `firstName`= ? ) ) // Example of use of multiple "and" and "or" with brackets $GOModel->where("status", 1); $GOModel->where("age",30,">="); $GOModel->openBrackets = "("; $GOModel->where("firstName", 'John'); $GOModel->andOrOperator = "OR"; $GOModel->where("firstName", 'bob'); $GOModel->closedBrackets = ")"; $result = $GOModel->select("emp"); //Example 4 - (query generated ->SELECT * FROM `emp` GROUP BY `age` ORDER BY `firstName` LIMIT 0,5 ) //groupby, order by and limit example $GOModel->groupByCols = array("age"); $GOModel->orderByCols = array("firstName"); $GOModel->limit = "0,5"; $result = $GOModel->select("emp"); //Example 5 - (query generated ->SELECT * FROM `emp` WHERE `firstName`LIKE ? AND `age` BETWEEN ? AND ? AND `empId`IN (?,?) ORDER BY `firstName` LIMIT 0,5 ) // LIKE, BETWEEN, IN, NOT IN example $GOModel->andOrOperator = "AND"; $GOModel->where("firstName", "Jo%", "LIKE"); $GOModel->where("age", array(10,50), "BETWEEN"); $GOModel->where("empId", array(30,50), "IN"); $GOModel->limit = "0,5"; $result = $GOModel->select("emp"); //Example 6 - (query generated ->SELECT count(*), concat(firstname, ' ' , lastname) AS `fullname` FROM `emp` ORDER BY `firstName` ) // Aggregate functions like count example $GOModel->columns = array("count(*), concat(firstName, ' ' , lastName) as fullName"); $result = $GOModel->select("emp"); //Example 7 - (query generated ->SELECT * FROM `emp` GROUP BY `firstName` HAVING sum(age)>10 ORDER BY `firstName` ) $GOModel->groupByCols = array("firstName"); $GOModel->havingCondition = array("sum(age)>10"); $result = $GOModel->select("emp"); //Example 8 - (query generated ->SELECT * FROM `wp_postmeta` WHERE `post_id` IN (select post_id from wp_posts where post_id>?) ) //use of subquery with where condition $GOModel->where_subquery("post_id", "select post_id from wp_posts where post_id>?", "IN", array(1)); $result = $GOModel->select("wp_postmeta"); //Example 9 - (query generated ->SELECT * FROM `emp` ORDER BY `firstName` LIMIT 0,3 ) $GOModel->fetchType = "OBJ"; $GOModel->limit = "0,3"; $result = $GOModel->select("emp");
Result/Output
By default, it returns data in associative array format. You can set return type using the fetch mode.
Debug
Option | Details | Example |
---|---|---|
$GOModel->getLastQuery(); | Return last query executed | SELECT * FROM `emp` ORDER BY `firstName` LIMIT 0,3 |
print_r($GOModel->error) | Print errors (if any) | |
$GOModel->totalRows; | Total rows returned for select query | 10 |