if语句在关联数组中不起作用

时间:2013-07-30 12:43:05

标签: php if-statement

我有以下问题。我正在尝试编写从csv文件导入数据的函数。如果有像'<>'这样的符号,请在价格列中的此文件中这意味着价格是美元,需要转换。我知道这个变量是以数字表示的。如何将其转换为字符串?或者为什么声明根本不起作用?这里始终是源代码。

$str='<>';
    if( $variant['price'] ==$variant['price'].$str)
    {
    $sql = mysql_query("SELECT rate_to FROM s_currencies WHERE id=1 LIMIT 0, 30 ");
  $course= mysql_fetch_row($sql);
//$rate=$course[0];
  $variant_price = $item['price']*$course[0];
  $variant['price']=$variant_price;
        }

请帮忙!

3 个答案:

答案 0 :(得分:3)

如果有条件,您发布的代码将不会进入。用代码进行检查。

For eg. if $variant['price'] = '1';

if ('1' == '1<>')
{
}

以上条件不会进入if语句。

答案 1 :(得分:1)

您需要检查该字符串是否存在,而不是使用当前的IF语句。 strpos会让您满意

if(strpos($variant['price'],$str) !== false)  // <> is present
{
    // run your sql code
} 

我还建议远离mysql_ *函数,因为它们已被弃用。查看带有绑定参数的PDO或mysqli查询。

答案 2 :(得分:1)

    $str='<>';
   if( stristr($variant['price'],$str){
        $sql  ="SELECT rate_to FROM s_currencies WHERE id=1 LIMIT 0, 30 ";
         $qry = mysql_query($sql);
         if ($qry && mysql_num_rows($qry)>0){
            $variant['price'] = (str_replace($str,'',$variant['price'])*mysql_result($qry,0,0));
        } else {
            echo 'error while converting:' . mysql_error();
        }
   }