使用PDO

时间:2016-11-01 11:50:12

标签: php mysql pdo

我正在尝试使用PDO Prepared Statement执行以下查询,但是当我调用$query->fetch();时,它会抛出异常SQLSTATE[HY000]: General Error

这是PHP代码(请注意,类Database - 或代码中的变量$db - 只是类PDO的简单包装器,因此所有PDO调用都必须完成使用$db->pdo->{some PDO function}();):

$db = new Database(); //Create a new object of type Database establishing a connection to the MySQL database
$query = $db->pdo->prepare("INSERT INTO `orders` (`order_type`, `item`, `amount`, `price`, `price_btc`, `status`, `timestamp`, `placed_by`, `secret`, `first_name`, `last_name`, `address_1`, `address_2`, `city`, `zip_code`, `country`, `state`, `phone_number`)
                         VALUES(:order_type, :item, :amount, :price, :price_btc, :status, :timestamp, :placed_by, :secret, :first_name, :last_name, :address_1, :address_2, :city, :zip_code, :country, :state, :phone_number);
                         SELECT * FROM `orders` WHERE `ID`=LAST_INSERT_ID();"); //Prepare the two queries to be executed

/*HERE IS SOME CODE TO BIND PLACEHOLDERS TO SOME VALUES*/

if(!$query->execute()){
    error(); //Handle the error and terminate execution
}
if($query->rowCount() != 1){
    error(); //Handle the error and terminate execution
}

$query->setFetchMode(PDO::FETCH_ASSOC);
$order = $query->fetch(); //THIS IS WHERE THE EXCEPTION IS THROWN!

我已尝试通过PHPMyAdmin手动执行查询,但它运行正常。我还读过PDO在同一个语句中不支持多个查询,但在运行$query->execute();时不应该抛出异常吗?

此外,$query->rowCount(); DOES返回1,但是当我尝试获取结果时,它会抛出一般错误异常。

我尝试过很多其他的事情,例如将SELECT语句替换为SELECT LAST_INSERT_ID();,但似乎没有任何效果。

感谢您的帮助!

3 个答案:

答案 0 :(得分:4)

运行你的第一个查询,即插入然后成功后获取最后一个insertid,然后在下一个查询中使用id。例如。

<?php



try {

        $db = new Database(); //Create a new object of type Database establishing a connection to the MySQL database


        $query = $db->prepare("INSERT INTO orders (order_type`, `item`, `amount`, `price`, `price_btc`, `status`, `timestamp`, `placed_by`, `secret`, `first_name`, `last_name`, `address_1`, `address_2`, `city`, `zip_code`, `country`, `state`, `phone_number`) VALUES(:order_type, :item, :amount, :price, :price_btc, :status, :timestamp, :placed_by, :secret, :first_name, :last_name, :address_1, :address_2, :city, :zip_code, :country, :state, :phone_number)");

        $query->execute(array( /* your values*/ ));


        $lastId = $db->lastInsertId(); // fetch last insert id, after success.


        $order = $db->prepare("SELECT * FROM `orders` WHERE `ID`=?");
        $order->bindValue(1, $lastId);
        $order->execute();
        //Fetch your records and display.


}
catch (PDOException $e) {
        echo "Error : " . $e->getMessage();

}

?>

我像你一样留下了部分代码,但重要的是首先运行插入然后收集最后一个

答案 1 :(得分:0)

你在PDO中是正确的(我相信任何PHP方法?)默认情况下不允许在一个PDO中进行多次查询。您可以阅读一些解决方法,例如:

PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

但是它们确实增加了SQL注入的风险,所以这是不明智的。

答案 2 :(得分:0)

您不需要多重声明。

所以只需逐个运行查询

$db = new Database(); //Create a new object of type Database establishing a connection to the MySQL database
$query = $db->pdo->prepare("INSERT INTO `orders` (`order_type`, `item`, `amount`, `price`, `price_btc`, `status`, `timestamp`, `placed_by`, `secret`, `first_name`, `last_name`, `address_1`, `address_2`, `city`, `zip_code`, `country`, `state`, `phone_number`)
                         VALUES(:order_type, :item, :amount, :price, :price_btc, :status, :timestamp, :placed_by, :secret, :first_name, :last_name, :address_1, :address_2, :city, :zip_code, :country, :state, :phone_number)"
); //Prepare the first query

/*HERE IS SOME CODE TO BIND PLACEHOLDERS TO SOME VALUES*/

$query->execute();

$order = $db->pdo->query("SELECT * FROM `orders` WHERE `ID`=LAST_INSERT_ID()")->fetch(PDO::FETCH_ASSOC);