Example #1 Execute a prepared statement with named placeholders <?php /* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour'); $sth->bindParam(':calories', $calories, PDO::PARAM_INT); $sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12); $sth->execute(); ?> Example #2 Execute a prepared statement with question placeholders <?php /* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?'); $sth->bindParam(1, $calories, PDO::PARAM_INT); $sth->bindParam(2, $colour, PDO::PARAM_STR, 12); $sth->execute(); ?>
大神,以下mysql 语句用来查询排名,在Mysql 中能执行,但是S2SH 框架中就不行,你能指点一下吗? select t.*,@rownum:=@rownum+1 AS rownum from (SELECT sum(yjb_yji) as total,yyryusername,yjb_yyry_id FROM yjb where month(createtime) =month(now()) and year(createtime)=year(now()) group by yjb_yyry_id order by total desc) t,(SELECT @rownum:=0) r 报此bug: org.hibernate.QueryException: Space is not allowed after parameter prefix ':'
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>
bindVars 方法将定义的符号与实际的变量关联起来
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>
Example #2 Execute a prepared statement with question placeholders
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>
select t.*,@rownum:=@rownum+1 AS rownum
from (SELECT sum(yjb_yji) as total,yyryusername,yjb_yyry_id
FROM yjb where month(createtime) =month(now()) and year(createtime)=year(now())
group by yjb_yyry_id order by total desc) t,(SELECT @rownum:=0) r 报此bug: org.hibernate.QueryException: Space is not allowed after parameter prefix ':'