Select


The select function is a simple wrapper for the SQL SELECT query.

The function:

function select($fields="*", $from="", $where="", $orderby="", $limit="") {
  if(!$from) return false;
  else {
    $table = $from;
    $where = ($where != "") ? "WHERE $where" : "";
    $orderby = ($orderby != "") ? "ORDER BY $orderby " : "";
    $limit = ($limit != "") ? "LIMIT $limit" : "";
    return $this->query("SELECT $fields FROM $table $where $orderby $limit;");
  }
}

To Use

$results = $modx->db->select("field1, field2", "table", "field = value", "field to order by", "limit value");

Example

$userId = $modx->getLoginUserID();
$table = $modx->getFullTableName("user_messages");
$messages = $modx->db->select("subject, message, sender", $table, "recipient = $userId", "postdate DESC", "10");

This will return the subject, message and sender of the latest 10 messages for the current user.