2017-03-02 81 views
1

我在这里有一些小问题。我是OOP的新手,所以这个问题听起来很愚蠢,但我找不到任何有用的信息。如何在OOP PDO中编写准备和执行语句?

我想登录到数据库,并把用户输入的值放在里面,我想问你,我应该在哪个地方编写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(); 
?> 
+3

您需要一次提出一个问题。编写类是一回事,执行准备好的语句是另一个 –

+0

哪一行是24? –

+0

$ query。=“'”。 implode(“','”,array_values($ data))。“')”; – HELPME

回答

1

我不是解释真的很好BRU,但我只看到,有很长一段时间后,没有回答。我为你创建了一个基本的类来使用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>'; 

这将显示乔的结果作为数组。

从我们的db中获得所有结果我们做了另一个功能来显示所有的结果。

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>'; 
+0

非常感谢你的时间,男人,它帮助了我很多! – HELPME

+0

伟大的人,我已经编辑了如何获取刚刚插入的数据的答案,请继续阅读本博客:https://phpdelusions.net/pdo –