如何将MySQL表检索到数组中然后在函数中使用数组内容

时间:2015-05-28 18:13:52

标签: php mysql arrays

我正在努力解决问题,但我无法让它发挥作用。我需要做以下事情:

  • 从MySQL表中提取数据
  • 将数据存储在数组中
  • 在函数中使用数组中的数据。

当前代码看起来像这样(当前从MySQL表中提取数据并对其进行迭代,将其放在表格中(纯粹是为了检查它是否有效)。

mysql_connect("localhost", "*****", "*****") or die(mysql_error());  

mysql_select_db("*****") or die(mysql_error());  


$data = mysql_query("SELECT batch FROM widgetBatches")  or die(mysql_error());  

Print "<table border cellpadding=3>";

while($info = mysql_fetch_array( $data ))  {  Print "<tr>";  
                                            Print "<th>Batch:</th> <td>".$info['batch'] . "</td> ";  
                                            Print "" . " </td></tr>";  
                                           }  Print "</table>";  

这是函数,$ widgetBatches需要从前面的代码中获取数组,但是我尝试过的任何工作都没有。 (Widget()还有更多内容,但它与此问题无关。提前致谢!

$uI = 550;

Widget($uI);     

function Widget($input) {
    $currentValue = $input;
    $i = 0;
    $widgetBatches = [250, 500, 1000, 2000];

    while ($currentValue > 0) {
        $i++;
        echo "Batch Number " . $i . "<br/>";
        echo "Widgets Left To Fulfill Order: " . $currentValue . "<br/>";
        $closest = ClosestBatch($widgetBatches, $currentValue);
        echo "Closest Batch Available: " . $closest . "<br/>";
        $currentValue = $currentValue - $closest;
        echo "Remaining Widgets: " . abs($currentValue) . "<hr/>";
    }

    echo "<br />";
}

0 个答案:

没有答案