gpt4 book ai didi

php - PDO 有问题,但没有错误

转载 作者:行者123 更新时间:2023-11-29 05:34:42 24 4
gpt4 key购买 nike

我正在玩 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!');

关于php - PDO 有问题,但没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11787753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com