I am playing around with PDO, but something weird is happening that I cannot seem to figure out. There are no errors being produced. Just nothing being inserted into the database.
我正在玩PDO,但有些奇怪的事情正在发生,我似乎无法弄明白。没有错误产生。只是没有插入数据库。
index.php
的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
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
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
);
When I navigate to index.php, "hello world" should be inserted into the database, but it's not. Can anyone find what I am doing wrong here?
当我导航到index.php时,应该将“hello world”插入到数据库中,但事实并非如此。任何人都可以在这里找到我做错的事吗?
3 个解决方案
#1
2
Change
更改
$dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);
to
至
$this->dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);
Change
更改
$stmt = $db->execute("INSERT INTO images (filename) VALUES (?)");
$stmt->bindParam(1, "hello world!");
$stmt->execute();
to
至
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!');
#2
1
Try this:
尝试这个:
index.php
的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
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;
}
}
}
The DatabaseCon should be acting as a factory to create your connection object.
DatabaseCon应该充当工厂来创建连接对象。
#3
0
Thanks to the help of @BenRowe, I found the issue and it is now fixed. A combination of using the $this keyword, and removing the "hello world!" portion from the call to bindParam() fixed it. Seems that the bindParam() method only takes a value based on reference. So I set a random variable $h to "hello world!," and changed bindParam(1, "hello world") to bindParam(1, $h).
感谢@BenRowe的帮助,我找到了问题,现在已修复。结合使用$ this关键字,删除“hello world!”调用bindParam()的部分修复了它。似乎bindParam()方法只接受基于引用的值。所以我将随机变量$ h设置为“hello world!”,并将bindParam(1,“hello world”)更改为bindParam(1,$ h)。
Thanks everyone :)
感谢大家 :)
#1
2
Change
更改
$dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);
to
至
$this->dbh = new PDO("mysql:host=" . $config['host'] . ";dbname=" . $config['dbname'], $config['dbuser'], $config['dbpass']);
Change
更改
$stmt = $db->execute("INSERT INTO images (filename) VALUES (?)");
$stmt->bindParam(1, "hello world!");
$stmt->execute();
to
至
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!');
#2
1
Try this:
尝试这个:
index.php
的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
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;
}
}
}
The DatabaseCon should be acting as a factory to create your connection object.
DatabaseCon应该充当工厂来创建连接对象。
#3
0
Thanks to the help of @BenRowe, I found the issue and it is now fixed. A combination of using the $this keyword, and removing the "hello world!" portion from the call to bindParam() fixed it. Seems that the bindParam() method only takes a value based on reference. So I set a random variable $h to "hello world!," and changed bindParam(1, "hello world") to bindParam(1, $h).
感谢@BenRowe的帮助,我找到了问题,现在已修复。结合使用$ this关键字,删除“hello world!”调用bindParam()的部分修复了它。似乎bindParam()方法只接受基于引用的值。所以我将随机变量$ h设置为“hello world!”,并将bindParam(1,“hello world”)更改为bindParam(1,$ h)。
Thanks everyone :)
感谢大家 :)