简体   繁体   English

回应PHP预备语句的最有效方法

[英]Most efficient way to echo PHP prepared statements

I have an SQL query which I have recently turned into a prepared statement for security purposes. 我有一个SQL查询,最近为了安全起见,我已将其转换为准备好的语句。 I have a query which returns many rows, each consisting of many columns. 我有一个查询,该查询返回许多行,每行包含许多列。 My question is how to echo the results using a while loop. 我的问题是如何使用while循环回显结果。 My example I have so far: 到目前为止,我的示例是:

$stmt = $conn->prepare("SELECT * FROM Customers 
                                  WHERE travel_Date >= ?
                                  AND   travel_Date <= ?
                                  ".$searchOption."
                                  LIMIT ? 
                                  OFFSET ?");
$todayDateFrom = $todayDate." 00:00:00";
$todayDateTo = $todayDate." 23:59:59";
$stmt->bind_param("ssii", $todayDateFrom, $todayDateTo, $limit, $offset);
$stmt->execute();

while ($stmt->fetch()) {
    //echo first name
    //echo surname
    //echo address
    //echo number
    //echo type
    //15 other things i need to print off
}

I'm not sure what the best way to do this is. 我不确定执行此操作的最佳方法是什么。 I have thought about: 我考虑过:

$stmt->bind_result($firstName, $surname, $address, //etc);

But I'm wondering if there's another alternative similar to unprepared statements: 但是我想知道是否还有另一种类似于未准备好的语句的替代方法:

while($row = mysqli_fetch_array($query)){
    echo $row['firstName'];
    echo $row['surname'];
    echo $row['address'];
    //etc
}

try this: 尝试这个:

$result = $stmt->get_result();
    while ($row = $result->fetch_array())
    {
        echo $row['firstName'];
        echo $row['surname'];
        echo $row['address'];
    }

try this - 尝试这个 -

$result_arr = array();
while($row = $stmt->fetch()){
  $record = array();
  $record['firstname'] = $row['firstname']; // if your db column contains firstname column other wise you can also use $row[0];
  $record['name'] = $row[1]; // end so on .......
  $result_arr[] = $record;
}

hope this will work for u... 希望这对你有用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM