Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将mysqli代码转换为PDO stmt?_Php_Sql_Mysqli_Pdo - Fatal编程技术网

Php 如何将mysqli代码转换为PDO stmt?

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

我正在尝试将此代码转换为PDO stmt。请帮帮我,我是新来的

您需要开发和pdo连接到您拥有的任何数据库。检查连接检查并与之连接

确保您已经安装了PDO库,请检查您的php信息

 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();
    }