PDO MYSQL PHP prepare()和execute()

时间:2015-11-06 07:42:57

标签: php mysql pdo execute prepare

它没有链接到数据库。我一直在尝试使用prepare()和execute()来让它在数据库上发布,但什么都没有显示我做错了什么?

<?php
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=phpexample', 'root', '');
    }
    catch (PDOException $e) 
    {
        echo "Connection error: " . $e->getMessage();
    }

    $query ="INSERT INTO products (id, sku, name) VALUES (:id, :sku, :title)";
    $stmt = $dbh->prepare($query);

    $id = NULL;
    $sku = 'MN873213';
    $title = 'Minty Mouthwash';

    //binding parameters
    $stmt->bindParam(':id', $id);
    $stmt->bindValue(':sku', $sku);
    $stmt->bindValue(':title', $title);

    //execute the query
    $stmt->execute();
?>

0 个答案:

没有答案