php如何将db查询的一部分放入函数中

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

几天来我一直在为这个问题绞尽脑汁,我终于让步了,现在正在寻求帮助。
我有这两套代码
这个函数调用下面的函数并将“isnull”传递给另一个函数,另一个函数显示数据。

<html>
<head>
   <link rel="stylesheet" href="style.css">
   <title>My PHP</title>
</head>
<body>

<div class="body">

 <?php 
    require_once 'ShowInventory.php';
    DBShowInventory("IS NULL");
 ?> 
 </div>
 </body>
</html>

<?php
function DBShowInventory($WhichInventory){

require_once 'connection.php';
$conn = Connect();

$result = $conn->query("select Description, PartNumber, Serial, Store, Cost, MSRP, DateReceived from inventory where DateSold $WhichInventory;");
echo "</br></br></br>\r\n";
echo "<table class=\"center\"> \r\n";
echo ' <tr>';
echo '    <th>Description</th>';
echo '    <th>Part Number</th>';
echo '    <th>Serial</th>';
echo '    <th>Store</th>';
echo '    <th>Cost</th>';
echo '    <th>MSRP</th>';
echo '    <th>DateReceived</th>';
echo " </tr> \r\n";

while ($row = $result->fetch_assoc()) {
              unset($Description, $PartNumber, $Serial, $Store, $Cost, $MSRP, $DateReceived);
              $Description = $row['Description'];
              $PartNumber = $row['PartNumber'];               
              $Serial = $row['Serial'];
              $Store = $row['Store'];
              $Cost = $row['Cost'];
              $MSRP = $row['MSRP'];
              $DateReceived = $row['DateReceived'];
              echo "<tr> \r\n";
              echo "<td>" .$Description. "</td> \r\n";
              echo "<td>" .$PartNumber. "</td> \r\n";
              echo "<td>" .$Serial. "</td> \r\n";
              echo "<td>" .$Store. "</td> \r\n";  
              echo "<td>";
              echo "₱";
              echo number_format($Cost,2,'.',',');
              echo "</td> \r\n";
              echo "<td>";
              echo "₱";
              echo number_format($MSRP,2,'.',',');
              echo "</td> \r\n";
              echo "<td>" .$DateReceived. "</td> \r\n";           
              echo "</tr> \r\n";

}
echo "</table> \r\n";
}
 ?>

我希望做的是翻转逻辑,让脚本在顶部显示,并使用底部的脚本只执行数据库查询。这样我就可以在不同的事情上反复使用这个函数。
我试着这样做,但我得到的是一个记录重复7次。

<?php
function DBLookup($DateSold){
require_once 'connection.php';
$conn = Connect();

$result = $conn->query("select Description, PartNumber, Serial, Store, Cost, MSRP, DateReceived from inventory where DateSold $DateSold;");

$row = (array) $result->fetch_assoc();
return $row;
}
?>

这是我想用来处理$row的脚本

<html>
<head></head>
<body>

<?php
require_once 'GetMultiRecordFromDB.php';
$row=DBLookup("IS NULL");

    echo "</br></br></br>\r\n";
    echo "<table class=\"center\"> \r\n";
    echo ' <tr>';
    echo '    <th>Description</th>';
    echo '    <th>Part Number</th>';
    echo '    <th>Serial</th>';
    echo '    <th>Store</th>';
    echo '    <th>Cost</th>';
    echo '    <th>MSRP</th>';
    echo '    <th>DateReceived</th>';
    echo " </tr> \r\n";

 foreach($row as $rowdata) {
              unset($Description, $PartNumber, $Serial, $Store, $Cost, $MSRP, $DateReceived);
              $Description = $rowdata['Description'];
              $PartNumber = $rowdata['PartNumber'];               
              $Serial = $rowdata['Serial'];
              $Store = $rowdata['Store'];
              $Cost = $rowdata['Cost'];
              $MSRP = $rowdata['MSRP'];
              $DateReceived = $rowdata['DateReceived'];
              echo "<tr> \r\n";
              echo "<td>" .$Description. "</td> \r\n";
              echo "<td>" .$PartNumber. "</td> \r\n";
              echo "<td>" .$Serial. "</td> \r\n";
              echo "<td>" .$Store. "</td> \r\n";  
              echo "<td>";
              echo "₱";
              echo number_format($Cost,2,'.',',');
              echo "</td> \r\n";
              echo "<td>";
              echo "₱";
              echo number_format($MSRP,2,'.',',');
              echo "</td> \r\n";
              echo "<td>" .$DateReceived. "</td> \r\n";           
              echo "</tr> \r\n";

}

    echo "</table> \r\n";

?>
</body>
</html>

感谢各位期待您的回复。

wj8zmpe1

wj8zmpe11#

更常见的做法是在循环中检索数据,并将一个记录数组传递回。。。

function DBLookup($DateSold){
    require_once 'connection.php';
    $conn = Connect();

    $result = $conn->query("select Description, PartNumber, Serial, Store, Cost, MSRP, DateReceived from inventory where DateSold $DateSold;");

    $data = array();
    while ($row = $result->fetch_assoc())  {
       $data[] = $row;
    }
    return $data;
}

您应该查看准备好的语句,以防止各种形式的注入,但当使用此替换时,它并不适合。确保你对这一点感到高兴,因为这是一个可以被滥用的事实。
如果将连接传递到方法中,也会“更好”,因此

function DBLookup($DateSold, $conn){

因为这允许您控制正在使用的连接,而不是总是创建自己的函数。

相关问题