PHP and PDO issues [closed]
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.
精彩评论