我试图在mySQL语句中传递AS
变量到变量,以便我可以处理结果。但是,我无法给这些变量赋值。
mySQL SELECT
语句如下
$sql = "SELECT min(ups) AS minups, max(ups) AS maxups, min(downs) AS mindowns, max(downs) AS maxdowns, min(score) AS minscore, max(score) AS maxscore, min(comments) AS mincomments, max(comments) AS maxcomments, min(totalVotes) AS mintotalvotes, max(totalVotes) AS maxtotalvotes FROM reddit WHERE movie = ':movie'";
$stmt = $conn->prepare( $sql );
$stmt->bindValue( ":movie", $redditMovies->reddit, PDO::PARAM_INT );
$stmt->execute();
$row = $stmt->fetch();
我想把它们赋值给这些变量
$minups = $row ['minups'];
$maxups = $row ['maxups'];
$rups = (int)($maxups - $minups);
print_r($rups);
$mindowns = $row ['mindowns'];
$maxdowns = $row ['maxdowns'];
$rdowns = (int)($maxdowns - $mindowns);
$minscore = $row ['minscore'];
$maxscore = $row ['maxscore'];
$rscore = (int)($maxscore - $minscore);
$mincomments = $row ['mincomments'];
$maxcomments = $row ['maxcomments'];
$rcomments = (int)($maxcomments - $mincomments);
$mintotalvotes = $row ['mintotalvotes'];
$maxtotalvotes = $row ['maxtotalvotes'];
$rtotalvotes = (int)($maxtotalvotes - $mintotalvotes);
我需要改变什么来解决这个问题?
尝试在您的查询中删除括号,如
movie = ':movie' to movie = :movie
使用此代码。use extract($row)
用于直接在$row数组中为键的赋值
$stmt = $conn->prepare("SELECT min(ups) AS minups, max(ups) AS maxups, min(downs) AS mindowns, max(downs) AS maxdowns, min(score) AS minscore, max(score) AS maxscore, min(comments) AS mincomments, max(comments) AS maxcomments, min(totalVotes) AS mintotalvotes, max(totalVotes) AS maxtotalvotes FROM reddit WHERE movie = ':movie'");
$stmt->bindValue( ":movie", $redditMovies->reddit, PDO::PARAM_INT );
$stmt->execute();
$row = $stmt->fetch();
extract($row);
echo $minups; // it prints the minups value from the result set $row.
$rups = (int)($maxups - $minups);
$rdowns = (int)($maxdowns - $mindowns);
$rscore = (int)($maxscore - $minscore);
$rcomments = (int)($maxcomments - $mincomments);
$rtotalvotes = (int)($maxtotalvotes - $mintotalvotes);
提取(行);用于为键的
赋值的例子:
$row['maxups']=5;
$row['minups']=2;
extract($row);
echo $maxups."-".$minups;
输出:5 - 2
试试这个
echo '<pre>';
print_r($row);
如果在数组中没有得到值,则检查查询是否有任何错误