获取php数组的值

h22fl7wq  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(391)

下面是php中的查询,我想从数组中提取值,并希望在带有foreach循环的函数中使用它们。但是我从数组中得到的值是错误的。我不知道这里出了什么问题,因为我是php的新手。

$sql="SELECT * FROM `mlm_pending_transactions` WHERE member_id = $_SESSION[member_id] and cycle=$cycle_number and credited=0 and order_type='upgrade_commission_cash'";
$result=mysql_query($sql);                                                                  
while($data_set = mysql_fetch_array($result))
{                                                                            
   foreach($data_set as $cash_pendings)
   {
      echo $cash_pendings["member_id"]."</br>";
      echo $cash_pendings['tran_amount']."</br>";
      echo $cash_pendings['tran_particulars']."</br>";
      echo $cash_pendings['id'];
    }
}
yi0zb3m4

yi0zb3m41#

您只需要mysql\u fetch\u array()返回的数组中的值

$sql="SELECT * FROM `mlm_pending_transactions` WHERE member_id = $_SESSION[member_id] and cycle=$cycle_number and credited=0 and order_type='upgrade_commission_cash'";
$result=mysql_query($sql);                                                                  
while($data_set = mysql_fetch_array($result))
{                                                                        
      echo $data_set["member_id"]."</br>";
      echo $data_set['tran_amount']."</br>";
      echo $data_set['tran_particulars']."</br>";
      echo $data_set['id'];
}

注意:如果字段名错误(区分大小写),则不会返回值。
如果不确定,可以使用print\r($data\u set)检查返回的字段名。
编辑-实际错误的更改。
绝对最小值。注意,没有测试,所以可能有一些拼写错误。

<?php

$sql="SELECT * 
    FROM `mlm_pending_transactions` 
    WHERE member_id = $_SESSION[member_id] 
    AND cycle = $cycle_number 
    AND credited = 0 
    AND order_type = 'upgrade_commission_cash'";
$result = mysql_query($sql);   

while($data_set = mysql_fetch_array($result))
{                                                                        
      echo $data_set["member_id"]."</br>";
      echo $data_set['tran_amount']."</br>";
      echo $data_set['tran_particulars']."</br>";
      echo $data_set['id'];

    $date = date("m/d/Y");
    $time = strtotime("now");

    $add_cash = $affiliate->add_fin_tran($data_set["member_id"],
                                $data_set["tran_amount"],
                                0,
                                $data_set["tran_particulars"],
                                "upgrade_commision_cash",
                                1);

    $sql2 = "UPDATE mlm_pending_transactions
        SET credit = 1,
        credited_date = '$date',
        credited_time = '$time'
        WHERE id = ".$data_set["id"];

    $result2 = mysql_query($sql2);

}

?>

但是,正如其他人所说,您使用的是不推荐使用的mysql*函数。此外,您对sql注入没有任何保护。
您应该使用一种较新的方法来访问数据库。通常是mysqli_x函数或基于pdo的函数。注意:要使用这些功能,还需要切换连接到数据库的函数(到匹配的函数)。
例如,使用mysqli函数(也可以使用面向对象的语法):-

<?php

$sql = "SELECT * 
    FROM `mlm_pending_transactions` 
    WHERE member_id = ?
    AND cycle = ?
    AND credited = ? 
    AND order_type = ?";
$statement = mysqli_prepare($db_connection, $sql); 
mysqli_stmt_bind_param($statement, 'iiis', $_SESSION['member_id'], $cycle_number, 0, 'upgrade_commission_cash');
mysqli_execute($statement);   
$result = mysqli_get_result($statement);   

while($data_set = mysqli_fetch_array($result, MYSQLI_ASSOC))
{                                                                        
      echo $data_set["member_id"]."</br>";
      echo $data_set['tran_amount']."</br>";
      echo $data_set['tran_particulars']."</br>";
      echo $data_set['id'];

    $date = date("m/d/Y");
    $time = strtotime("now");

    $add_cash = $affiliate->add_fin_tran($data_set["member_id"],
                                $data_set["tran_amount"],
                                0,
                                $data_set["tran_particulars"],
                                "upgrade_commision_cash",
                                1);

    $sql2 = "UPDATE mlm_pending_transactions
        SET credit = ?,
        credited_date = ?,
        credited_time = ?
        WHERE id = ?";

    $statement2 = mysqli_prepare($db_connection, $sql2);    
    mysqli_stmt_bind_param($statement2, 'issi', 1, $date, $time, $data_set["id"]);
    $result2 = mysqli_execute($statement2);   

}

?>

使用面向对象syntax:-

<?php

$sql = "SELECT * 
    FROM `mlm_pending_transactions` 
    WHERE member_id = ?
    AND cycle = ?
    AND credited = ? 
    AND order_type = ?";
$statement = $db_connection->prepare($sql); 
$statement->bind_param('iiis', $_SESSION['member_id'], $cycle_number, 0, 'upgrade_commission_cash');
$statement->execute();   
$result = $statement->get_result();   

while($data_set = $result->fetch_array(MYSQLI_ASSOC))
{                                                                        
      echo $data_set["member_id"]."</br>";
      echo $data_set['tran_amount']."</br>";
      echo $data_set['tran_particulars']."</br>";
      echo $data_set['id'];

    $date = date("m/d/Y");
    $time = strtotime("now");

    $add_cash = $affiliate->add_fin_tran($data_set["member_id"],
                                $data_set["tran_amount"],
                                0,
                                $data_set["tran_particulars"],
                                "upgrade_commision_cash",
                                1);

    $sql2 = "UPDATE mlm_pending_transactions
        SET credit = ?,
        credited_date = ?,
        credited_time = ?
        WHERE id = ?";

    $statement2 = $db_connection->mysqli_prepare($sql2);    
    $statement2->bind_param('issi', 1, $date, $time, $data_set["id"]);
    $result2 = $statement2->execute();   

}

?>

相关问题