代码有效,但" WHERE"没有按'吨

时间:2018-01-05 07:09:45

标签: php mysql

我有以下PHP / MySQL代码可以运行 - 但是" WHERE"注释掉的条款不会。

如果我运行它们,我会收到以下错误信息:

"**Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SCW_Files\ReadAllBowls.php on line 25**"

为什么我收到此错误?

此代码读取数据库scw-db的一个表,找到txtSourceWood值,然后在第二个表中搜索匹配值,然后读取该行的数据。然后将结果格式化为csv格式,显示在屏幕上,并保存到csv文件中以便在Excel中使用。

如果我可以使用" WHERE"在第二个选择中工作的子句,我是否需要使用循环代码来获取txtSourceWood匹配,或者我可以直接在代码中使用它吗?

<?php
$conn = mysql_connect("localhost","****","*****");
if(! $conn )
  {
  die('Could not connect: ' . mysql_error());
  }

$sql = 'SELECT intProductID FROM tblsplintersbowlinventory';

mysql_select_db('scw-db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

$price = "0.00";
$result = mysql_query("SELECT * FROM tblsplintersBowlinventory ORDER BY intProductID");// WHERE txtVenue == 'Shoreline 2017'");

$Sqfile = fopen("SCW_Sq.csv", "w");
$Sq_record = 'ProductID, ProductBowlCode, Venue, ProductBowlPriceSold, SalesTax, ProductBowlProceeds, SourceWood, WoodIDcode, Price, Tax, Shipping, Cost, Profit';
fwrite($Sqfile, $Sq_record);
fwrite($Sqfile, "\n");              

while ($row = mysql_fetch_array($result))
{
    if($row['curProductBowlPriceSold'] > $price) {
        $found = strpos($row['txtVenue'], '2017');
        if($found) {
            $result_wood = mysql_query("SELECT * FROM oldtblsplinterswoodinventory ORDER BY oldtxtWoodIDcode");// WHERE oldtxtWoodIDcode = $row['intProductID']");
            while ($row_wood = mysql_fetch_array($result_wood))
            {
                if($row_wood['oldtxtWoodIDcode'] == $row['txtSourceWood']){
                    $Profit = $row['curProductBowlProceeds'] - $row_wood['oldcurCost'];
                    echo "{$row['intProductID']}, {$row['txtProductBowlCode']}, {$row['txtVenue']}, {$row['curProductBowlPriceSold']}, {$row['curSalesTax']}, {$row['curProductBowlProceeds']}, ";
                    echo "{$row_wood['oldtxtWoodIDcode']}, {$row_wood['oldcurPrice']}, {$row_wood['oldcurTax']}, {$row_wood['oldcurShipping']}, {$row_wood['oldcurCost']}  <br>";

                    $Sq_record = '#'.$row['intProductID'].','.$row['txtProductBowlCode'].','.$row['txtVenue'].','.$row['curProductBowlPriceSold'].','.$row['curSalesTax'].','.$row['curProductBowlProceeds'].','.$row['txtSourceWood'].','.$row_wood['oldtxtWoodIDcode'].','.$row_wood['oldcurPrice'].','.$row_wood['oldcurTax'].','.$row_wood['oldcurShipping'].','.$row_wood['oldcurCost'].','.$Profit;
                    fwrite($Sqfile, $Sq_record);
                    fwrite($Sqfile, "\n");              
                }
            }
        }
    }
};
echo "Done\n";
fclose($Sqfile);
mysql_close($conn);
?

1 个答案:

答案 0 :(得分:1)

首先,您必须将排序前的位置放在第二位,其次,您的查询格式应该如下所示。

$result_wood = mysql_query("SELECT * FROM oldtblsplinterswoodinventory WHERE oldtxtWoodIDcode = '".$row['intProductID']."' ORDER BY oldtxtWoodIDcode");// WHERE oldtxtWoodIDcode = $row['intProductID']");

跳这会帮助你。