Please enable JavaScript to view this site.

Navigation: » No topics above this level «

DAL method: Query (Deprecated)

Scroll Prev Next More

 

Deprecated

This function is deprecated and we recommend using the DB:Query() function from Database API.

 

Selects records from the database, sorts the data by the orderby field(s), and returns the recordset.

Syntax

Query(where, orderby)

Arguments

where

a WHERE clause. Example: "ID=19".

orderby

one or more fields used to sort the recordset by.

Return value

Returns the recordset on success or FALSE on error.

Example 1

Select all records where name contains 'Jim'. To fetch a returned row as an associative array use db_fetch_array function.

 

global $dal;
$tblUsers = $dal->Table("UsersTable");
$rs = $tblUsers->Query("Name like '%Jim%'","Email DESC");
while( $data = db_fetch_array($rs) )
{
  echo $data["fieldName"]."<br>";
}

 

Note: the db_fetch_array function is deprecated, and we recommend using the fetchAssoc function instead.

 

The corresponding SQL query:

 

select * from UsersTable where Name like '%Jim%' order by Email DESC

Example 2

Select and print all orders for John Sample.  To fetch a returned row as an associative array, use the db_fetch_array function.

 

global $dal;
$tblOrders = $dal->Table("OrdersTable");
$rs = $tblOrders->Query("Customer='John Sample'","OrderID DESC");
while ($data = db_fetch_array($rs))
{
  echo "Order ".$data["OrderID"]." was placed ".$data["OrderDate"]." by ".$data["Customer"]."<br>";
}

 

Note: the db_fetch_array function is deprecated, and we recommend using the fetchAssoc function instead.

 

The corresponding SQL query:

 

select * from OrdersTable where Customer like 'John Sample'
order by OrderID DESC

Examples:

Example: Show data from master table on details view/edit/add page

Example: Before deleting a record check if related records exist

Example: Redirect to user info edit page

Example: Show list of customer orders

See also:

QueryResult object: fetchAssoc

DAL Method: QueryAll

DAL Method: CustomQuery

DB API: Query

Database API

About Data Access Layer

 

Created with Help+Manual 7 and styled with Premium Pack Version 3 © by EC Software