PHP脚本无法识别缺少的变量

时间:2013-07-14 06:29:46

标签: php mysql mysqli php-5.4

我正在编写一个小脚本来将产品从csv文件上传到我的网站。我遇到了一个问题,我的if($result) echo "hi";会在$ result存在时回显,但是如果我放if(!result)if(empty($result)if(!isset(result))则不会回显结果...在我的productlist.txt代码列表中,我有3个产品代码,其中2个在数据库中,当我有if($ result)时它回显2'hi'但是如果我有if(!result)没有什么是回声,当它应该回应1嗨。

while($data = fgetcsv($handle)) {

    //Gets products to be added from txt file.
    $codes = explode("\n", file_get_contents("productlist.txt"));

    foreach($codes as $code) {

         if($data[3] == $code) {

            $name  = $data[2];
            $model = $data[3];
            $description = strip_tags($data[6]);
            echo $price = round($data[7] + 20 + (0.05 * $data[7])) - 0.05; echo "</br>";
            $image = "data/W_Sexy_Lingerie_Series_Lingerie-Supplies/" . $model . ".jpg";

            //Check to see if product already exists
            $query = "SELECT `product_id` FROM `oc_product` WHERE `model`='$model'";
            $result = $con->query($query);

            if(empty($result)) echo "hi";

            break;

            //Insert product information
            $query = "INSERT INTO `oc_product` (`model`, `quantity`, `image`, `price`, `status`) VALUES
            ('$model', '5', '$image',  '$price', 1)";

            if (!$con->query($query)) {
                echo $con->error;
            }

            //Get product ID
            $query = "SELECT `product_id` FROM `oc_product` WHERE `model`='$model'";
            $result = $con->query($query);

            $rows = $result->fetch_array(); 

            $product_id = $rows[0];

            //Insert product category
            $query = "INSERT INTO `oc_product_to_category` (`product_id`, `category_id`) VALUES ('$product_id', 59)";

            if (!$con->query($query)) {
                echo $con->error;
            }           

            //Insert product description
            $query  = "INSERT INTO `oc_product_description` (`product_id`, `language_id`, `name`, `description`,";                  $query .= " `meta_description`, `meta_keyword`)";
            $query .= " VALUES ('$product_id', 1, '$name', '$description', '$description', '$description')";

            if (!$con->query($query)) {
                echo $con->error;
            }           

         }

     }

}

}

newProducts($handle,$con);

1 个答案:

答案 0 :(得分:0)

if( $result->num_rows == 0 ) echo "hi";