I struggle a lot with UPDATE query when used in class. Other queries like SELECT, ALTER work just fine. Query itsef works well in phpmyadmin and if PDO is defined in procedural way (but exactly the same as in class) the very same query works. Struggling with it for days and now I simply give up hence my question here. Error reporting is on
在课堂上使用UPDATE查询时我很挣扎。其他查询,如SELECT,ALTER工作得很好。查询itsef在phpmyadmin中运行良好,如果PDO是以程序方式定义的(但与类完全相同),则查询工作正常。苦苦挣扎了几天,现在我只是放弃了我的问题。错误报告已启用
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also error reporting is set as attribute upon creating an object but no errors thrown for UPDATE.
此外,错误报告在创建对象时设置为属性,但没有为UPDATE抛出错误。
Environment is localhost on Linux Mint 17.3. Working code (but not for UPDATE):
环境是Linux Mint 17.3上的localhost。工作代码(但不是UPDATE):
class MySQLDatabase {
private $PDO;
private $error;
private $host = _DB_SERVER_;
private $dbname = _DB_NAME_;
private $user = _DB_USER_;
private $passwd = _DB_PASSWD_;
private $stmt;
public function __construct(){
try {
$this->PDO = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', ''.$this->user.'', ''.$this->passwd.'');
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function update_records(){
$query = 'UPDATE ps_2orders
SET
rozliczone = 1
WHERE
id_order = 1535';
$this->query($query);
$this->execute();
}
public function query($query){
$this->stmt = $this->PDO->prepare($query);
}
public function execute() {
return $this->stmt->execute();
}
Now working code:
现在工作代码:
try {
$pdo = new PDO('mysql:host='._DB_SERVER_.';dbname='._DB_NAME_.';charset=utf8', ''._DB_USER_.'', ''._DB_PASSWD_.'');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit('Database connection error.');
}
$query = $pdo->prepare("UPDATE ps_2orders
SET
rozliczone = 1
WHERE
id_order = 1535");
$query->execute();
What I tried so far: mostly adapting the query but obviously its not query causing the problem. It must have something to do with with object.
到目前为止我尝试过的方法:主要是调整查询,但很明显它不是导致问题的查询。它必须与对象有关。
Any help would be much appreciated.
任何帮助将非常感激。
1 个解决方案
#1
0
$query = 'UPDATE ps_2orders SET rozliczone=:rozliczone WHERE id_order = :id_order';
$stmt = $this->PDO->prepare($query);
$stmt->execute(array(':rozliczone' => 1, ':id_order' => 1535));
This will work. because you have made pdo object with :
这会奏效。因为你已经制作了pdo对象:
$this->PDO = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', ''.$this->user.'', ''.$this->passwd.'');
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
so you have to use by $this->PDO->pdoFunctions
.
所以你必须使用$ this-> PDO-> pdoFunctions。
#1
0
$query = 'UPDATE ps_2orders SET rozliczone=:rozliczone WHERE id_order = :id_order';
$stmt = $this->PDO->prepare($query);
$stmt->execute(array(':rozliczone' => 1, ':id_order' => 1535));
This will work. because you have made pdo object with :
这会奏效。因为你已经制作了pdo对象:
$this->PDO = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', ''.$this->user.'', ''.$this->passwd.'');
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
so you have to use by $this->PDO->pdoFunctions
.
所以你必须使用$ this-> PDO-> pdoFunctions。