开发者

PHP and PDO issues [closed]

开发者 https://www.devze.com 2023-03-12 06:41 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

So I am a beginner with PDO and I am having trouble doing something that seems to be simple. I cannot get my table to update, and it is not throwing me any errors. Take a look:

<?php

  $host   = 'localhost';
  $dbname = 'postGal';
  $user   = 'user';
  $pass   = 'pass';

  $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
  $DB->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );   

  catch(PDOException $e) {  
    echo "Reported Error";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
  }

  class person {  
    public $f_name;  
    public $l_name;   

    function __construct($f,$l) {  
        $this->f_name = $f;  
        $this->l_name = $l;  
    }  
  }  

  $person = new person('John','Doe');  

  $STMT = $DB->("INSERT INTO users (f_nam开发者_如何学Pythone, l_name) value (:f_name, :l_name)");  
  $STMT->execute((array)$person); 

?>

Also, I know the table and everything is working properly as I can execute this same statement with mysql or mysqli. Thanks!


You have to embrace the block of code which might throw an exception in a try-block to catch a thrown exception.

Manual

Also it has to say VALUES not VALUE in your insert-statement.

You are also totally mis-applying prepared statements. See the manual for some simple examples. For example you have to invoke the prepare-method at some point and your array-cast is pretty odd, not sure what you get doing that.

I am also smelling a troll here.


Maybe that catch wants to be a set_error_handler. $DB->("INSERT misses the prepare method. Finally I do not think you can cast an object to an array just like this. Maybe you wanted get_object_vars and manipulate it to become the desired array (keys starting with colons) but then what's the point of object? Just use an array.

0

精彩评论

暂无评论...
验证码 换一张
取 消