我使用PHP“ fgetcsv”功能读取了CSV文件,并将产品从csv添加/更新到我的opencart商店。为此,我必须检查产品是否存在。

我编写了以下函数来检查模型中是否存在产品:

function existProduct($searchValue)
{
    $sql ="SELECT `product_id` FROM ".DB_PREFIX."product WHERE `model`='".$searchValue."'";

    $result2 = $this->db->query($sql);

    if ($result2->num_rows > 0)
    {
        return $result2->row['product_id'];
    }

    return false;
}


并通过以下方式调用控制器:

$productID = $this->model_tool_cust_import->existProduct($searchValue);


当产品存在于产品表中时,它返回空结果,但是如果我在单引号中传递字符串值,则它可以工作,例如'YS16034137'

 $productID = $this->model_tool_cust_import->existProduct('YS16034137');


我尝试使用trim,但是没有用。

当使用var_dump($searchValue)时,我得到以下输出:

string 'Y�S�1�6�0�3�4�1�3�7' (length=19)


另外,当我尝试echo $searchValue ;时,我得到的输出是YS16034137

最佳答案

当我从csv读取列值时,得到了解决方案,它在字符串的每个字符之间添加了“空字符”(\ 0)。我不知道它来自哪里,但是我使用str_replace(“ \ 0”,“”,$ value)从字符串中删除了空字符,然后为我工作。

关于php - 在opencart中,当表中存在记录时,SELECT查询将提供空结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34460239/

10-12 07:25