我在这里遇到一些问题。我是OOP的新手,所以问题可能听起来很愚蠢,但我找不到任何有用的信息。
我正在尝试登录数据库并将用户输入的值放在其中,我想问你,我应该在哪个地方编写PDO准备和执行语句?我知道他们是需要的,但我不知道如何正确地写它...除此之外,我还得到一个错误“类PDO的对象无法转换为第24行的字符串”。感谢您的帮助,这是我的代码:
<?php
class Connection {
public $connection;
public $dbHost = 'localhost';
public $dbName = 'employees';
public $charset = 'charset=utf8';
public $dbUser = 'root';
public $dbPassword = '';
public function __construct() {
try {
$this->connection = new PDO ("mysql:host=$this->dbHost;$this->dbName;$this->dbUser;
$this->dbPassword");
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo "There is something wrong with the database".$e->getMessage();
}
}
function insertUserValues($tableName, $data) {
$query = "INSERT INTO ".$tableName."(";
$query .= implode (",",array_keys($data)).') VALUES (';
$query .= "'" . implode ("','",array_values($data))."')";
}
}
$users = new Connection();
?>
答案 0 :(得分:3)
我对解释问题并不是很了解,但我发现很长一段时间后都没有答案。我已经创建了一个基本类,您可以使用PDO插入值,我希望它能指向正确的方向,我也会为您分享一些有用的链接。
首先是连接。
我可以看到你已经在班上完成了连接,但下面是正确的最佳pdo连接。
$host = '127.0.0.1';
$db = 'YourDatabase';
$user = 'YourDBUser';
$pass = 'YourDBPass';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$dbh = new PDO($dsn, $user, $pass, $opt);
这就是你如何设置正确的PDO连接。 dns 代表数据来源名称上述https://phpdelusions.net/pdo#dsn的参考资料这家伙更好地解释了它。你需要知道的一切。
现在你如何将这种联系与你的班级一起放在一起?
好吧,我将创建一个文件收集pdoClass.php并从该类开始工作。
<?php
class Connection
{
private $host = "127.0.0.1";
private $dbName = "YourDB";
private $user = "YourUser";
private $pass = "YourPass";
private $charset = 'utf8';
private $dbh;
private $error;
private $stmt;
//connection
public function __construct()
{
$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName . ";charset=" . $this->charset;
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
);
try {
// setup connection
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
//catch any errors
catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
//prepare statement
public function insertUserValues($query)
{
$this->stmt = $this->dbh->prepare($query);
}
//bind values
public function bind($param, $value, $type = null)
{
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
//actual value binding
$this->stmt->bindValue($param, $value, $type);
}
//execute statement
public function run()
{
return $this->stmt->execute();
}
}
?>
基本上你需要设置数据库以及要在数据库中插入的函数,我试图评论一些部分。
现在使用这个类创建index.php或者你喜欢什么。然后包括班级
<?php
include'pdoClass.php';
$users = new Connection();
$users->insertUserValues('INSERT INTO test (name, age, description) VALUES(?,?,?)');
$users->bind(1, 'User'); //bind each value
$users->bind(2, 391); // bind
$users->bind(3, 'This is a value');
if($database->run()){
echo "record inserted";
}
?>
完成,如果您有任何疑问或想要我解释任何问题,请随时在下面发表评论,我会尽力帮助您。
编辑:如果您需要获取结果,您还可以在课程中创建一个新功能,
单行:
public function SingleRow(){
$this->run();
return $this->stmt->fetch();
}
请参阅我们使用fetch();
仅获取一行。大多数人在获取结果时都会像fetch(PDO::FETCH_ASSOC)
一样获取它们,但由于我们进行了正确的连接并在连接中定义了我们的默认提取模式,因此我们不需要使用fetch()
;
要在index.php文件中显示这些结果,您将采用以下方法:
$users->insertUserValues("SELECT name, age, description FROM test WHERE name = :name");
$users->bind(':name','joe');
$row = $users->SingleRow();
echo '<pre>';
print_r($row);
echo '</pre>';
这会将乔的结果显示为数组。
从我们的数据库中获取所有结果
我们执行另一个功能来显示所有结果。
public function All(){
$this->run();
return $this->stmt->fetchall();
}
现在我们看到了差异,我们使用fetchall()
因为我们想要所有结果。
$users->insertUserValues("SELECT * FROM test");
$row = $users->All();
echo '<pre>';
print_r($row);
echo '</pre>';