Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP如何将DB查询的一部分放入函数中_Php_Mysql - Fatal编程技术网

PHP如何将DB查询的一部分放入函数中

PHP如何将DB查询的一部分放入函数中,php,mysql,Php,Mysql,几天来,我一直在为此事绞尽脑汁。我终于让步了,现在正在寻求帮助 我有这两套代码可以工作 这个函数调用下面的函数,并将“IS NULL”传递给另一个函数,另一个函数显示数据 <html> <head> <link rel="stylesheet" href="style.css"> <title>My PHP</title> </head> <body> <div class="body">

几天来,我一直在为此事绞尽脑汁。我终于让步了,现在正在寻求帮助

我有这两套代码可以工作

这个函数调用下面的函数,并将“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";
}
 ?>

我的PHP
我希望做的是翻转逻辑,让脚本在顶部显示,并使用底部的脚本只执行数据库查询。这样我就可以一遍又一遍地为不同的事情使用这个函数

我试过这样做,但我得到的只是一张重复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;
}
您应该查看准备好的语句,以防止各种形式的注入,但当使用此语句进行替换时,它不适合。确保你对这一点感到满意,因为这可能会被滥用

如果将连接传递到方法中,也会“更好”,因此

function DBLookup($DateSold, $conn){ 

这使您可以控制正在使用的连接,而不是总是创建自己的功能。

请参见非常感谢Nigel,它非常有效。我为问这样一个新手问题感到难过。我又是一个新手了。