1. Getting Started
      1. Basic Installation
      2. What is MODx
    2. Content Editing
      1. Editing Documents
      2. MODx Tags
        1. Document Variables
      3. Terminology
      4. The Manager
      5. Who Should Read This
    3. Designing
      1. Adding Chunks
      2. Adding MODx Tags
        1. Resource Fields
      3. Adding Snippets
      4. Document Caching
      5. Template Basics
    4. Administration
      1. Friendly URL Solutions
      2. Manager Users
        1. Manager Roles And Groups
        2. Reset your Password - Unblock your User
        3. Why Manager Users, Roles and Groups
      3. Moving Site
      4. Taking sites down for maintenance
      5. Upgrading
      6. Web Users
        1. Creating a Web User
        2. Web User Groups and Document Groups
        3. Why Web Users and Groups
    5. Developer's Guide
      1. API Reference
        1. DBAPI
          1. delete
          2. escape
          3. getInsertId
          4. query
          5. select
          6. update
        2. Document Object
        3. DocumentParser Object
          1. addEventListener
          2. changeWebUserPassword
          3. documentContent
          4. documentGenerated
          5. documentIdentifier
          6. documentListing
          7. documentMethod
          8. documentObject
          9. getAllChildren
          10. getCachePath
          11. getChildIds
          12. getDocumentChildren
          13. getDocumentChildrenTVarOutput
          14. getDocumentChildrenTVars
          15. getLoginUserID
          16. getLoginUserName
          17. getLoginUserType
          18. getManagerPath
          19. getParent
          20. getParentIds
          21. getUserData
          22. hasPermission
          23. isBackend
          24. isFrontend
          25. logEvent
          26. maxParserPasses
          27. minParserPasses
          28. regClientCSS
          29. runSnippet
          30. table_prefix
          31. tstart
          32. webAlert
      2. Chunks
      3. Modules
        1. How to create and run a module from within the Content Manager
        2. Managing module dependencies
        3. Setting up configuration parameters
        4. Writing the module code
      4. Plugins
      5. Snippets
      6. Template Variables
        1. (at) Binding
          1. (at)CHUNK
          2. (at)DIRECTORY
          3. (at)DOCUMENT
          4. (at)EVAL
          5. (at)FILE
          6. (at)INHERIT
          7. (at)SELECT
          8. What are (at) Bindings
        2. Creating a Template Variable
        3. What are Template Variables
        4. Widgets
          1. Misc. Widget
          2. DataGrid Widget
          3. Floater Widget
          4. Hyperlink Widget
          5. Marquee Widget
          6. RichTextBox Widget
          7. Ticker Widget
          8. Viewport Widget
          9. What are Widgets

select

Last edited by JP DeVries on Aug 9, 2013.

API:DB:select

API Quick reference
Variable name: select
Modx versions: 0.9.x + Evolution
Input parameters: ([string $fields [, string $from [, string $where [, string $orderby [, string $limit]]]]])
Return if successful: resource
Return type: resource
Return on failure: false
Object parent: DocumentParser -> DBAPI

Description

resource select([string $fields [, string $from [, string $where [, string $orderby [, string $limit]]]]])

The select function is a simple wrapper for the SQL SELECT query. select() sends a query to the currently active database, $this->conn as created when connect() is called. This function is analogous to mysql_query() except the query string is broken up into 5 parts. Only the $from field is required for a meaningful result.

*Parameter $fields are the field or column name(s) that you want returned. If left blank it will default to all (*).
*$from is the table to query. If left blank function will return false.
*$where is the full string of the WHERE clause of the mysql query. Leave blank to do no WHERE matching.
*$orderby, if needed, can either be 'ASC' or 'DESC'.
*$limit is the limit of the number of results to return, leave blank for all

Usage / Examples

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

This example login function uses the select function to get the id of a user given the username and password.

function login($username, $password)
{
   global $modx;
   $username = $modx->db->escape($username); // this should have been done with the POST
   $password = $modx->db->escape($password); // values before passing them to the function
 
   $res = $modx->db->select("id", $modx->getFullTableName('web_users'), 
      "username='" . $username ."' AND password='".md5($password)."'");
   if($modx->db->getRecordCount($res))
   {
      $id = $modx->db->getValue($res);
      $_SESSION['userid'] = $id;
      //other log in things...
   }
   else
   {
      //incorrect login
   }
}

Example2:

$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.

[getRecordCount](), query(), [getRow](), [makeArray]().

Notes

One could easily use mysql_query($qry) instead of this function, however it is preferred to use this function, especially for modx table calls, to ensure future compatibility should their be any changes to the way modx queries the database (eg: changing to mysqli_query(), or using another database type altogether).

Function Source

File: manager/includes/extenders/dbapi.class.inc.php
Line: 169

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");
   }
}

Suggest an edit to this page.