我正在开发一个php程序。我不能使这段代码工作:
$ref="demo_3";
$ident=comprobarExisteProd($ref);
function comprobarExisteProd ($refer)
{
$id_product=0;
$sqlpr = 'SELECT * FROM `'._DB_PREFIX_.'product` WHERE `reference`=(string)$refer';
$res = Db::getInstance()->executeS($sqlpr);
var_dump($res);
echo "<br>";
if (!$res){
echo "The product does not exist in database";
echo "<br>";
} else{
$id_product=$res[0]['id_product'];
$precio=$res[0]['price'];
echo "The product exist with identifier: : ";
echo $id_product;
echo "<br>";
echo "The product price is: ";
echo $precio;
echo "<br>";
}
return id_product;
}
但是,如果我改变:
$sqlpr = 'SELECT * FROM `'._DB_PREFIX_.'product` WHERE `reference`=(string)$refer';
与另一个:
$sqlpr = 'SELECT * FROM `'._DB_PREFIX_.'product` WHERE `reference`="demo_3"';
然后代码正常工作。
如何使用参数comprobarExisteProd($refer)
?
$refer
我已经多次更改了代码,但它不起作用。
答案 0 :(得分:0)
您无法在声明中转换变量$ refer。声明应该是:
$sqlpr = 'SELECT * FROM `'._DB_PREFIX_.'product` WHERE `reference`="'.(string)$refer.'"';