【问题标题】:In opencart, a SELECT query gives an empty result when record is present in table在 opencart 中,当表中存在记录时,SELECT 查询会给出空结果
【发布时间】:2015-12-25 05:25:36
【问题描述】:

我使用 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。

【问题讨论】:

  • 根据您的问题,不清楚您的代码应该做什么。您主要描述什么不起作用。 How do I ask a good question? - 考虑编辑您的问题以强调预期的行为。
  • 你的表名是什么?? .DB_PREFIX 是故意的还是只是 DB_PREFIX ? DB_PREFIX 之前的点。
  • DB_PREFIX 在打开的购物车中获取前缀,表名为 oc_product 。 echo $ql 我得到了输出“ SELECT product_id FROM oc_product WHERE model='YS16034137'”

标签: php mysql csv opencart2.x


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2019-10-04
    • 2010-10-15
    • 1970-01-01
    相关资源
    最近更新 更多