1

我正在玩 PDO,但发生了一些我似乎无法弄清楚的奇怪事情。没有产生错误。只是没有任何东西被插入到数据库中。

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

类.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();
        }
    }
}

配置文件

<?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”插入到数据库中,但事实并非如此。谁能在这里找到我做错了什么?

4

3 回答 3

2

改变

$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!');
于 2012-08-03T00:43:12.960 回答
1

试试这个:

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

类.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 应该充当创建连接对象的工厂。

于 2012-08-03T01:02:42.090 回答
0

感谢@BenRowe 的帮助,我发现了这个问题,现在已经解决了。使用 $this 关键字和删除“hello world!”的组合。调用 bindParam() 的部分修复了它。似乎 bindParam() 方法仅采用基于引用的值。所以我将随机变量 $h 设置为“hello world!”,并将 bindParam(1, "hello world") 更改为 bindParam(1, $h)。

谢谢大家 :)

于 2012-08-03T01:05:43.553 回答