我在addtocart函数中遇到问题后,在mysql中将此代码更改为PDO
function get_product_name($pid){
$result=mysql_query("select title from topic where id=$pid");
$row=mysql_fetch_array($result);
return $row['title'];
}
function addtocart($pid,$q){
if($pid<1 or $q<1) return;
if(is_array($_SESSION['cart'])){
if(product_exists($pid)) return;
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['productid']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
}
else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid']=$pid;
$_SESSION['cart'][0]['qty']=$q;
}
}
我想将Mysql更改为PDO
function get_product_name($pid){
$result = $DB_con->prepare("SELECT * FROM `topic` WHERE id=$pid");
$result->execute();
$row = $result->fetchAll();
return $row['title'];
}
function addtocart($pid,$q){
if($pid<1 or $q<1) return;
if(is_array($_SESSION['cart'])){
if(product_exists($pid)) return;
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['productid']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
}
else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid']=$pid;
$_SESSION['cart'][0]['qty']=$q;
}
}
但是在将代码更改为PDO后,函数addtocart无法正常工作