Elgg\Database::sanitizeString PHP Method

sanitizeString() public method

Sanitizes a string for use in a query
Deprecation: Use query parameters where possible
public sanitizeString ( string $value ) : string
$value string Value to escape
return string
    public function sanitizeString($value)
    {
        $quoted = $this->getConnection('read')->quote($value);
        if ($quoted[0] !== "'" || substr($quoted, -1) !== "'") {
            throw new \DatabaseException("PDO::quote did not return surrounding single quotes.");
        }
        return substr($quoted, 1, -1);
    }

Usage Example

Example #1
0
 /**
  * Returns SQL appropriate for relationship joins and wheres
  *
  * @todo add support for multiple relationships and guids.
  *
  * @param string $column               Column name the GUID should be checked against.
  *                                     Provide in table.column format.
  * @param string $relationship         Type of the relationship
  * @param int    $relationship_guid    Entity GUID to check
  * @param bool   $inverse_relationship Is $relationship_guid the target of the relationship?
  *
  * @return mixed
  * @access private
  */
 public function getEntityRelationshipWhereSql($column, $relationship = null, $relationship_guid = null, $inverse_relationship = false)
 {
     if ($relationship == null && $relationship_guid == null) {
         return '';
     }
     $wheres = array();
     $joins = array();
     $group_by = '';
     if ($inverse_relationship) {
         $joins[] = "JOIN {$this->db->getTablePrefix()}entity_relationships r on r.guid_one = {$column}";
     } else {
         $joins[] = "JOIN {$this->db->getTablePrefix()}entity_relationships r on r.guid_two = {$column}";
     }
     if ($relationship) {
         $wheres[] = "r.relationship = '" . $this->db->sanitizeString($relationship) . "'";
     }
     if ($relationship_guid) {
         if ($inverse_relationship) {
             $wheres[] = "r.guid_two = '{$relationship_guid}'";
         } else {
             $wheres[] = "r.guid_one = '{$relationship_guid}'";
         }
     } else {
         // See #5775. Queries that do not include a relationship_guid must be grouped by entity table alias,
         // otherwise the result set is not unique
         $group_by = $column;
     }
     if ($where_str = implode(' AND ', $wheres)) {
         return array('wheres' => array("({$where_str})"), 'joins' => $joins, 'group_by' => $group_by);
     }
     return '';
 }
All Usage Examples Of Elgg\Database::sanitizeString