我使用 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;
}
并通过以下方式调用 Controller :
$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/