Please enable JavaScript to view this site.

Navigation: Advanced topics > Programming topics > Database API > Methods

Database API:Select()

Scroll Prev Next More

 

Retrieves data from the database.

Syntax

DB.Select(table, values, sort)
// or
DB.Select(table, where, sort)

Arguments

table

the table from which the data is selected.

values

an array with values that define WHERE condition of the query.

where

the WHERE condition of the query.

sort

an array with ORDER BY fields.

Return value

An array with the result of the Select query.

Example 1

Retrieve all data from the Cars table where the make is Toyota and the model is RAV4.

 

dynamic data = XVar.Array();
data["make"]="Toyota";
data["model"]="RAV4";
 
dynamic record;
dynamic rs = DB.Select("Cars", data);
while(record = rs.fetchAssoc())
{
  MVCFunctions.Echo(record["id"]);
  MVCFunctions.Echo(record["make"]);
}

Example 2

Retrieve all data from the Cars table where the price is less than 20,000.

 

dynamic record;
dynamic rs = DB.Select("Cars", "Price<20000");
while(record = rs.fetchAssoc())
{
  MVCFunctions.Echo(record["id"]);
  MVCFunctions.Echo(record["make"]);
}

 

 

Example 3

 

Sorting data by two fields in ascending or descending mode.

 

 
DB.Select( "tableName", XVar.Array(), XVar.Array( "field1", "field2" ) );
 
// The code above produces the following SQL query:
// SELECT * FROM tableName order by field1, field2
 
DB.Select(
"tableName",
XVar.Array(),
XVar.Array(
  XVar.Array( "field1", "a" ),
  XVar.Array( "field2", "d" )
)
);
 
// The code above produces the following SQL query:
// SELECT * FROM tableName order by field1 ASC, field2 DESC

 

See also:

QueryResult object: fetchAssoc()

Database API: Exec()

Database API: Query()

Database API: Insert()

Database API: Update()

Database API: Delete()

Database API: PrepareSQL()

QueryResult object: fetchNumeric()

QueryResult object: value()

Using SQL variables

About Database API