acao Posted October 15, 2013 at 02:54 PM Report #529040 Posted October 15, 2013 at 02:54 PM boas necessito ajuda em retornar os valores num select com order by: segue o cod: $stmt = $this->cnn->prepare("SELECT DISTINCT $this->campo1 FROM $this->tabela WHERE $this->campoParam1 = ? ORDER BY $this->campoOrderBy1 = ? LIMIT ?, ?"); $stmt->bindValue('1', $valorGet->getCampoParam1(), PDO::PARAM_INT); $stmt->bindValue('2', $valorGet->getCampoOrderBy1(), PDO::PARAM_STR); $stmt->bindValue('3', $valorGet->getLimitInicio(), PDO::PARAM_INT); $stmt->bindValue('4', $valorGet->getLimit(), PDO::PARAM_INT); $stmt->execute(); se no campoOrderBy1 colocar o = ? retorna-me todos os registo mas não ordenados. se no campoOrderBy1 colocar sem = não retorna nada. estou a utilizar o valor para o campo order by (DESC ou ASC); por isso o problema deve ser no order bY. cumps acao
HappyHippyHippo Posted October 15, 2013 at 04:24 PM Report #529055 Posted October 15, 2013 at 04:24 PM (edited) eu nem sei como é que tens retorno de registos, isto porque o que tens é um erro de sintaxe do SQL. verifica aqui como é que se escreve a regra de ordenação http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html $stmt = $this->cnn->PREPARE(" SELECT DISTINCT {$this->campo1} FROM {$this->tabela} WHERE {$this->campoParam1} = ? ORDER BY {$this->campoOrderBy1} ? -- sem o = LIMIT ?, ? "); Edited October 15, 2013 at 04:25 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
acao Posted October 15, 2013 at 11:35 PM Author Report #529115 Posted October 15, 2013 at 11:35 PM eu nem sei como é que tens retorno de registos, isto porque o que tens é um erro de sintaxe do SQL. verifica aqui como é que se escreve a regra de ordenação http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html $stmt = $this->cnn->PREPARE(" SELECT DISTINCT {$this->campo1} FROM {$this->tabela} WHERE {$this->campoParam1} = ? ORDER BY {$this->campoOrderBy1} ? -- sem o = LIMIT ?, ? "); boas @HappyHippyHippo obrigado pelo retorno. estive a visualizar o link e não vi erro de ordenação. fiz a alteração indicada mas continua igual. é aconselhado utilizar as chaves?. eu já resolvi mas foi da maneira mais fácil, foi colocar lá manualmente o DESC, resolveu mas sem a opção de colocar as ASC. eu presumo que o erro seja o param eu estou a utilizar o PARAM_STR, já fiz tambem a conversão para string ao adicionar o valor, mas sem resultados positivos. cumps acao
HappyHippyHippo Posted October 16, 2013 at 12:59 AM Report #529119 Posted October 16, 2013 at 12:59 AM eu já resolvi mas foi da maneira mais fácil, foi colocar lá manualmente o DESC, resolveu mas sem a opção de colocar as ASC. isso porque ao ser feito por parâmetro, o valor (neste caso DESC e ASC) será ladeado dos delimitadores de string (') IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
acao Posted October 16, 2013 at 01:35 PM Author Report #529198 Posted October 16, 2013 at 01:35 PM isso porque ao ser feito por parâmetro, o valor (neste caso DESC e ASC) será ladeado dos delimitadores de string (') boas obrigado pelas respostas, pois lá terá que ficar assim. referente às {} é mesmo aconselhado usa-las ? , cumps acao
HappyHippyHippo Posted October 16, 2013 at 01:59 PM Report #529203 Posted October 16, 2013 at 01:59 PM http://php.net/manual/en/language.types.string.php Variable parsing When a string is specified in double quotes or with heredoc, variables are parsed within it. There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort. The complex syntax can be recognised by the curly braces surrounding the expression. Simple syntax If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. For anything more complex, you should use the complex syntax. Complex (curly) syntax This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$. conclusão : não estorva e elimina alguns erros que podem aparecer por esquecimento ou distracção no uso de expressões dentro de uma string IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
acao Posted October 16, 2013 at 02:18 PM Author Report #529210 Posted October 16, 2013 at 02:18 PM boas obrigado cumps acao
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now