PHP 如何将部分数据库查询放入函数中

标签 php mysql

几天来我一直在努力解决这个问题,我终于屈服了,现在正在寻求帮助。

我有这两组有效的代码

这个函数调用下面的函数并将“IS NULL”传递给另一个函数,另一个函数显示数据。

<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>

谢谢大家期待您的回复。

最佳答案

您通常会在循环中检索数据并将记录数组传回...

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;
}

您应该查看准备好的语句以防止各种形式的注入(inject),但是当使用它进行替换时,它不适合。确保您对这容易被滥用这一事实感到满意。

如果将连接传递给方法也会“更好”,所以

function DBLookup($DateSold, $conn){ 

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

关于PHP 如何将部分数据库查询放入函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52380249/

相关文章:

mysql - 将持久对象委托(delegate)给 Redis,使用 MySQL 回退

php - 在 WooCommerce 商店页面上显示库存中可用的产品属性值

php - 如何高效地存储多类别、多属性的数据?

php - Zend 静态助手

PHP MYSQL文件内容转义问题

php - 如何从Windows Live Mail导出电子邮件

php - 这个 SQL 查询有什么问题?

PHPmailer 破坏 HTML 正文消息。 (漏洞?)

php - 反射异常,类不存在

MySQL查询获取列的总和