【问题标题】:Call sql query for each value of array in php为php中数组的每个值调用sql查询
【发布时间】:2017-07-17 13:23:55
【问题描述】:

我刚开始学习 PHP,想为数组中的所有值调用 SQL 更新查询。但我不知道在 PHP 中执行它。

 <?php
  $array = array("12345","23456","34567");
//now here how to call each value of arrayy
foreach ($array as $arr) {
    $sql_points1    = "UPDATE user_earning SET points = points + '80' WHERE user_number = '//how to get value of number from array.'";
    $result_points1 = $conn->query($sql_points1);

    if ($result_points1 === TRUE) {
        echo "Current points added   ";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

}
?>

【问题讨论】:

  • 我们需要查看您的数组结构以确定如何从中获取值。如果数据是敏感的,请弥补。
  • 好的,我会更新我的问题。
  • 您只是在问如何从数组中获取值吗? php.net/manual/en/language.types.array.php
  • 不问如何从数组中获取值并放入mysql查询以更新值。
  • $array = '12345' 不是数组,但这是$array = array(1,2,3,4,5)

标签: php mysql sql-server


【解决方案1】:

在您的 Foreach 循环中,当前数组元素的值被分配给 $arr,因此您可以在查询中使用它。

$sql_points1 = "UPDATE user_earning SET points = points + '80' WHERE user_number = '$arr'";

【讨论】:

    【解决方案2】:

    请使用以下代码:

    <?php
      $array = array("12345","23456","34567");
    //now here how to call each value of arrayy
    foreach ($array as $arr) {
        $sql_points1    = "UPDATE user_earning SET points = points + '80' WHERE user_number = $arr";
        $result_points1 = $conn->query($sql_points1);
    
        if ($result_points1 === TRUE) {
            echo "Current points added   ";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多