Querying Specific Columns

Sometimes you don't need all the table columns, so the POSQL must change from using the wildcard symbol to identifying which columns you want to fetch. Updating the example statement previously mentioned, you'll pull three columns for each record, the ID of the artist, the name of artist, and the genre the artist belongs to. The new statement looks like this:

SELECT "artists"."id", "artists"."artist_name", "artists"."genre" FROM "artists"

Using the from() method's second parameter, pass in an array. You can create an array containing string elements representing the individual column names (see Listing 5-26).

Listing 5-26. Identifying Specific Columns to Fetch

* Test - Object Oriented Select Statement

public function testoostatementAction() {

//Create DB object require_once "Db/Db_Db.php"; $db = Db_Db::conn();

//Create the statement

//SELECT "artists"."id", "artists"."artist_name", "artists"."genre" //FROM "artists"

//Determine which columns to retrieve. $columns = array('id', 'artist_name', 'genre'); $statement = $select->from('artists', $columns);

//Compare Statement echo $statement->_toString();

//Supress the View


For ease of use, create a $columns array variable that contains a list of all the columns you want to fetch from the table and pass it into the from() method's second parameter. Each element in the array represents a column name in the table you are fetching records from. Load the URL http://localhost/artist/testoostatement and look at the created statement. It contains the columns specified now. So how do you execute the statements?

Was this article helpful?

0 0

Post a comment