0

所以我的 php 脚本中有一堆这样的:

//get current values to add to new ones
    $sql = "SELECT `pageViews` FROM $table WHERE `appName` = '$appName'";
    $result = mysql_query($sql);
    if (!$result) {
     die("Invalid query: " .mysql_error());
    }
    $totalPoints += mysql_result($result, 0, 0);


    //get current values to add to new ones
    $sql = "SELECT `appTime` FROM $table WHERE `appName` = '$appName'";
    $result = mysql_query($sql);
    if (!$result) {
     die("Invalid query: " .mysql_error());
    }
    $appRunTime += mysql_result($result, 0, 0);

    //get current values to add to new ones
    $sql = "SELECT `soundsPlayed` FROM $table WHERE `appName` = '$appName'";
    $result = mysql_query($sql);
    if (!$result) {
     die("Invalid query: " .mysql_error());
    }
    $lifesLost += mysql_result($result, 0, 0);

是否可以将所有这些查询组合成一个并仍然检索每个单独的信息,即将它们设置为一个变量?

谢谢。

4

1 回答 1

1

你在寻找这样的东西吗?

$sql = "SELECT pageViews, appTime, soundsPlayed 
          FROM $table 
         WHERE appName = '$appName'";

$result = mysql_query($sql);
if (!$result) {
 die("Invalid query: " .mysql_error());
}
if ($row = mysql_fetch_assoc($result)) {
    $totalPoints += $row['pageViews'];
    $appRunTime  += $row['appTime'];
    $lifesLost   += $row['soundsPlayed'];
}

附带说明:不要插入查询字符串,而是使用带有or的准备好的语句mysqli_*PDO

于 2013-09-09T02:38:03.810 回答