"注意:PDOStatement类的对象无法转换为int"在插入操作期间

时间:2016-03-16 09:57:09

标签: php pdo

我的代码正常运行,但运行时if if声明有 NOTICE 。我试图解决它,但我不能。 注意错误:注意:第30行的D:\ wamp \ www \ pdo \ addOrders.php中无法将PDOStatement类的对象转换为int 这是我的代码:

<?php
$host = 'localhost';
    $dbname = 'test';
    $username = 'root';
    $password = '';

    try 
    {
        $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
       /* echo "Connected to $dbname at $host successfully.<br/>"; */
    }
    catch (PDOException $e) 
    {
        echo "<h3>Error:</h3>". $e->getMessage();
    }


$customer_id=addslashes($_GET['customer_id']);

$t=time();
$d=date("Y-m-d",$t);


$sql= "INSERT INTO `test`.`orders` (`customer_id`, `date`) VALUES ('$customer_id', '$d');";

$result =$conn->query($sql);
$lastid = $conn->lastInsertId();

if($result==1)
{

    $query = "select * from `orders` where customer_id='$customer_id'";
    $result = $conn->query($query);
    $dbvalue = $result->fetch(PDO::FETCH_OBJ);
    $customer_id=$dbvalue->customer_id;
    $date=$dbvalue->date;


    $details = array(
    'status'=>'sucess', 
    'message'=>'customer added sucessfully',
    'id' => $lastid, 
    'customer_id' => $customer_id,
    'date' => $date,
    );

    echo  json_encode($details);
}
else
{
    $detail = array(
    'status'=>'unsucess',   
    'message'=>'cannot add customer',

    );

    echo  json_encode($detail);
}
?>

1 个答案:

答案 0 :(得分:1)

trunc(systimestamp - interval '5' minute, 'minute') 在成功时返回$conn->query($sql)个对象,或在失败时返回PDOStatement。比较false是无稽之谈。这在== 1时代是无稽之谈,在PDO时代仍然是无稽之谈。您想测试真实性的结果,而不是数字。

这很简单:

mysql_query