0

我试图将ASmySQL 语句中的变量分配给变量,以便处理结果。但是我无法将 分配给这些变量。

mySQLSELECT语句如下

$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);

我需要改变什么来解决这个问题?

4

3 回答 3

1

尝试删除查询中的括号,例如,

movie = ':movie' to movie = :movie
于 2013-03-26T13:15:55.313 回答
1

使用此代码。useextract($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

于 2013-03-26T13:24:22.713 回答
0

尝试这个

echo '<pre>';
 print_r($row);

如果数组中没有值,请检查查询是否有任何错误

于 2013-03-26T13:18:21.977 回答