0

i have this function that shows an autosuggest in a form:

function searchbyId($params) {

    $input = strtolower($params['input']);
    $len = strlen($input);
    $limit = isset($params['limit']) ? (int) $params['limit']:25;
    $items=array();
    $sql='SELECT DISTINCT nIdentidad, CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre FROM tarjeta_indent WHERE nIdentidad LIKE \''.$input.'%\' ORDER BY nIdentidad LIMIT '.$limit;
    $resp=db_query($sql);
    if($resp && db_num_rows($resp)){
        while(list($nIdentidad)=db_fetch_row($resp)) {
            //$name=(strpos($name,'@')===false)?$name:'';
            $items[] ='{"id": "'.$nIdentidad.'", "value": "'.$nIdentidad.'"}';

        }
    }
    $result= '{"results": ['.implode(", ", $items).']}';
    return $result;
}

but only works if i change the query to this:

$sql='SELECT DISTINCT nIdentidad FROM tarjeta_indent WHERE nIdentidad LIKE \''.$input.'%\' ORDER BY nIdentidad LIMIT '.$limit;

How can i do the concat part?

Thanks.

2
  • 1
    and this doesn't work ... how? remember that select distinct applies to the ENTIRE row. it's not "distinct nIdentidad values". Commented Nov 16, 2012 at 14:48
  • try a GROUP BY nIdentidad query instead of DISTINCT. Commented Nov 16, 2012 at 15:18

2 Answers 2

1

I feel like this is decidedly simple, but you have a syntax error in your concat statement; you're using single quotes to escape strings but the PHP string is defined with single quotes:

$sql='SELECT DISTINCT nIdentidad, CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre FROM tarjeta_indent WHERE nIdentidad LIKE \''.$input.'%\' ORDER BY nIdentidad LIMIT '.$limit;

How about this?

$sql = sprintf(<<<SQL
    SELECT
      DISTINCT nIdentidad,
      CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre
    FROM tarjeta_indent
    WHERE nIdentidad LIKE '%s'
    ORDER BY nIdentidaa
    LIMIT %d
SQL
  , mysql_real_escape_string($input), $limit);

If I'm right about db_query and you're using Drupal, try this instead (for Drupal 7):

$sql = <<<SQL
    SELECT
      DISTINCT nIdentidad,
      CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre
    FROM tarjeta_indent
    WHERE nIdentidad LIKE :input
    ORDER BY nIdentidaa
    LIMIT :limit
SQL;
$resp = db_query($sql, array(':input' => $input, ':limit' => $limit));

This would be the Drupal 6 version:

$sql = <<<SQL
    SELECT
      DISTINCT nIdentidad,
      CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre
    FROM tarjeta_indent
    WHERE nIdentidad LIKE %s
    ORDER BY nIdentidaa
    LIMIT %d
SQL;
$resp = db_query($sql, $input, $limit);
Sign up to request clarification or add additional context in comments.

Comments

0

CONCAT will work with strings/varchar, but not with numbers. So if primerNombre and segundNombre are INT data types, CONCAT will fail. Try using the CONVERT function:

SELECT DISTINCT nIdentidad, 
       CONCAT( CONVERT(primerNombre, char(8)), ' ', 
               CONVERT(segundoNombre, char(8))  ) as nombre

2 Comments

primerNombre and segundNombre are text. When i run my query in mysql works fine.
Try select concat('whatever', 1, 'voodoo') -> no convert needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.