几天来我一直在努力解决这个问题,我终于屈服了,现在正在寻求帮助。
我有这两组有效的代码
这个函数调用下面的函数并将“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/