我有以下问题。我正在尝试编写从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;
}
请帮忙!
您发布的代码不会进入if条件。使用代码进行检查。
For eg. if $variant['price'] = '1';
if ('1' == '1<>')
{
}
上述条件不会进入if语句。
您需要检查该字符串是否存在,而不是使用当前的if语句。strpos会给你想要你需要的
if(strpos($variant['price'],$str) !== false) // <> is present
{
// run your sql code
}
我还建议不要使用mysql_*函数,因为它们已被弃用。查看带有绑定参数的PDO或mysqli查询。
$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();
}
}