PDO MYSQL PHP prepare() and execute()

你离开我真会死。 提交于 2019-12-12 00:19:45

问题


its not linking up to the database. I've been trying to use prepare() and execute() to get it to post on the database but nothing shows up what am I doing wrong?

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

来源:https://stackoverflow.com/questions/33561770/pdo-mysql-php-prepare-and-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!