Php 如何将mysqli代码转换为PDO stmt?
我正在尝试将此代码转换为PDO stmt。请帮帮我,我是新来的 您需要开发和pdo连接到您拥有的任何数据库。检查连接检查并与之连接 确保您已经安装了PDO库,请检查您的php信息Php 如何将mysqli代码转换为PDO stmt?,php,sql,mysqli,pdo,Php,Sql,Mysqli,Pdo,我正在尝试将此代码转换为PDO stmt。请帮帮我,我是新来的 您需要开发和pdo连接到您拥有的任何数据库。检查连接检查并与之连接 确保您已经安装了PDO库,请检查您的php信息 if($_REQUEST['action'] == 'addToCart' && !empty($_REQUEST['id'])){ $productID = $_REQUEST['id']; // get product details $query
if($_REQUEST['action'] == 'addToCart' && !empty($_REQUEST['id'])){
$productID = $_REQUEST['id'];
// get product details
$query = $db->query("SELECT * FROM products WHERE id = ".$productID);
$row = $query->fetch_assoc();
$itemData = array(
'id' => $row['id'],
'name' => $row['name'],
'price' => $row['price'],
'qty' => 1
);
$insertItem = $cart->insert($itemData);
$redirectLoc = $insertItem?'viewCart.php':'index.php';
header("Location: ".$redirectLoc);
}
$db
是pdo
连接还是mysqli
?到目前为止你做了什么prepare(“SELECT*FROM products WHERE id=?”
。execute(array($productID))
您可能希望向我们展示您的数据库类,以便我们可以看到它在做什么
try
{
$conn =new PDO("sqlsrv:Server=$this->hostName;Database=$this->dbName", "$this->userName", "$this->password");
$productID = $_REQUEST['id'];
$sql = "SELECT * FROM products WHERE id=?";
$stmt->bindParam(1, $productID);
$stmt = $conn->prepare($sql);
$this->result = $stmt->execute();
if (!$this->result)
{
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$itemData = array(
'id' => $row['id'],
'name' => $row['name'],
'price' => $row['price'],
'qty' => 1
);
}
}
$insertItem = $cart->insert($itemData);
$redirectLoc = $insertItem?'viewCart.php':'index.php';
header("Location: ".$redirectLoc);
}
catch(Exception e)
{
echo $e->getMessage();
exit();
}