Loading...
 

DAL method: Query()

Selects records from the database sorting data by the orderby field or fields and return the recordset.
Syntax
Query(where, orderby)
Arguments
where - WHERE clause. Example: ID=19.
orderby - one or more fields used to sort the recordset.
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:
PHP
global $dal;
$tblUsers = $dal->Table("UsersTable");
$rs = $tblUsers->Query("Name like '%Jim%'","Email DESC");
$data = db_fetch_array($rs);
ASP
set rs = dal.Table("UsersTable").Query("Name like '%Jim%'","Email DESC")
The corresponding SQL query:
SQL
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 db_fetch_array function:
PHP
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>";
ASP
set rs = dal.Table("OrdersTable").Query("Customer='John Sample'","OrderID DESC")
while not rs.eof
  Response.write "Order " & rs("OrderID") & " was placed " & rs("OrderDate") & " by " & rs("Customer") & "<br>"
  rs.movenext
wend
The corresponding SQL query:
SQL
select * from OrdersTable where Customer like 'John Sample'
  order by OrderID DESC

See also

  • Example: Show data from master table on detail 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
  • Method: QueryAll
  • Method: CustomQuery