簡體   English   中英

有PDO的麻煩,但沒有錯誤

[英]Having Trouble with PDO, but No Errors

我正在玩PDO,但有些奇怪的事情正在發生,我似乎無法弄明白。 沒有錯誤產生。 只是沒有插入數據庫。

的index.php

<?php
error_reporting(E_ALL); 
require('includes/classes.php');
require_once('includes/config.php');

$db = new DatabaseCon();
$db->dbConnect($config);

$stmt = $db->prepare("INSERT INTO images (filename) VALUES (?)");
$stmt->bindParam(1, "hello world!");
$stmt->execute();
?>

classes.php

<?php
error_reporting(E_ALL);
require('config.php');

// Connect to database
// Does not handle anything else
class DatabaseCon {
    public $dbh;

    // Method to connect to database
    function dbConnect($config) {
        try {
            $dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);
            //$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
}

config.php文件

<?php
error_reporting(E_ALL);

$config = array(
    'host' => 'localhost',  // Database hostname (usually localhost)
    'dbuser' => 'admin_mp', // Database username
    'dbpass' => 'mypassword here', // Database password
    'dbname' => 'mpdb' // Database name
);

當我導航到index.php時,應該將“hello world”插入到數據庫中,但事實並非如此。 任何人都可以在這里找到我做錯的事嗎?

更改

$dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);

$this->dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);

更改

$stmt = $db->execute("INSERT INTO images (filename) VALUES (?)");
$stmt->bindParam(1, "hello world!");
$stmt->execute();

error_reporting(E_ALL);
$stmt = $db->dbh->prepare("INSERT INTO images (filename) VALUES (?)");
if (!$stmt) die ('prepare() failed!');
$h = "hello world!";
$rv = $stmt->bindParam(1, $h);
if (!$rv) die ('bindParam() failed!');
$rv = $stmt->execute();
if (!$rv) die ('execute() failed!');

嘗試這個:

的index.php

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
require('includes/classes.php');
require_once('includes/config.php');

$db = new DatabaseCon();
$db = $db->dbConnect($config);

$stmt = $db->prepare("INSERT INTO images (filename) VALUES (?)");
$stmt->bindParam(1, "hello world!");
$stmt->execute();
?>

classes.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require('config.php');

// Connect to database
// Does not handle anything else
class DatabaseCon {

    // Method to connect to database
    function dbConnect($config) {
        try {
            $dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);
            //$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
            return $dbh;
        } catch (PDOException $e) {
            echo $e->getMessage();
                    return false;
        }

    }
}

DatabaseCon應該充當工廠來創建連接對象。

感謝@BenRowe的幫助,我找到了問題,現在已修復。 結合使用$ this關鍵字,刪除“hello world!” 調用bindParam()的部分修復了它。 似乎bindParam()方法只接受基於引用的值。 所以我將隨機變量$ h設置為“hello world!”,並將bindParam(1,“hello world”)更改為bindParam(1,$ h)。

感謝大家 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM