Php PDOStatement$语句执行http 500错误

Php PDOStatement$语句执行http 500错误,php,mysql,Php,Mysql,我的PDOStatement$语句生成HTTP 500,我不知道为什么!这是我的密码: final class ContatoDao { private $db = null; public function __destruct() { //Fecha a conexão $this->db = null; } private function getDb() { if ($this->db !== null) { return $th

我的PDOStatement$语句生成HTTP 500,我不知道为什么!这是我的密码:

final class ContatoDao {

private $db = null;

public function __destruct() {
    //Fecha a conexão
    $this->db = null;
}

private function getDb() {
    if ($this->db !== null) {
        return $this->db;
    }
    $config = Config::getConfig("db");
    try {
        $this->db = new PDO($config['dsn'], $config['username'], $config['password']);
    } catch (Exception $ex) {
        throw new Exception('Conexao com o banco de dados falhou: ' . $ex->getMessage());
    }
    return $this->db;
}

private function execute($sql, Contato $contato) {
    $statement = $this->getDb()->prepare($sql);
    $this->executeStatement($statement, $this->getParams($contato));
    if (!$contato->getId()) {
        return $this->findById($this->getDb()->lastInsertId());
    }
    if (!$statement->rowCount()) {
        throw new NotFoundException('Contato with ID "' . $contato->getId() . '" não existe.');
    }
    return $contato;
}

private function executeStatement(PDOStatement $statement, array $params) {
    if (!$statement->execute($params)) {
        self::throwDbError($this->getDb()->errorInfo());
    }
}

private function query($sql) {
    $statement = $this->getDb()->query($sql, PDO::FETCH_ASSOC);
    if ($statement === false) {
        self::throwDbError($this->getDb()->errorInfo());
    }
    return $statement;
}

/**
 * Recebe id para efetuar a busca 
 * @param type $id 
 * @return retorna o objeto \Contato
 */
public function findById($id) {
    $row = $this->query('SELECT * FROM contatos WHERE deleted = 0 and id = ' . (int) $id)->fetch();
    if (!$row) {
        return null;
    }
    $contato = new Contato();
    ContatoMapper::map($contato, $row);
    return $contato;
}

/**
 * Salva o objeto Contato na base de dados.
 * @param Contato $contato
 * @return type
 */
private function insert(Contato $contato) {
    $contato->setDeletado(false);
    $sql = '
        INSERT INTO contatos (id, nome, email, msg, phone, deletado)
            VALUES (:id, :nome, :email, :msg, :phone, :deletado)';
    return $this->execute($sql, $contato);
}

/**
 * Efetua atualização.
 * @param Contato $contato para fazer atualização.
 * @return type void
 */
private function update(Contato $contato) {

    $sql = '
        UPDATE contatos SET
            nome = :nome,
            email = :email,
            msg = :msg,
            phone = :phone,
            deletado = :deletado,
        WHERE
            id = :id';
    return $this->execute($sql, $contato);
}

/**
 * function para salvar Contato $contato base de dados
 * @param function recebe Contato $contato.
 * @return type void
 */
public function save(Contato $contato) {
    if ($contato->getId() === null) {
        return $this->insert($contato);
    }
    return $this->update($contato);
}

private function getParams(Contato $contato) {
    $params = array(
        ':id' => $contato->getId(),
        ':nome' => $contato->getNome(),
        ':email' => $contato->getEmail(),
        ':msg' => $contato->getMensagem(),
        ':phone' => $contato->getPhone(),
        ':deletado' => $contato->getDeletado(),
    );
    return $params;
}

private static function throwDbError(array $errorInfo) {
    // TODO log error, send email, etc.
    throw new Exception('DB error [' . $errorInfo[0] . ', ' . $errorInfo[1] . ']: ' . $errorInfo[2]);
}
}
我正在尝试将对象Contato保存到Mysql中,它可以工作,但之后出现错误HTTP500

如果你想看到错误,就去看看

通过下面的代码,我调用该方法将对象保存在我的小项目中,我将放置try/catch

  <?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$errors = array();
$contato = new Contato();

if (array_key_exists('cancel', $_POST)) {
    Utils::redirect('home');
} elseif (array_key_exists('save', $_POST)) {

    $data = array(
        'nome' => $_POST['contato']['nome'],
        'email' => $_POST['contato']['email'],
        'msg' => $_POST['contato']['msg'],
        'phone' => $_POST['contato']['phone'],
    );

    ContatoMapper::map($contato, $data);

    $errors = ContatoValidator::validade($contato);

    if (empty($errors)) {
        Utils::sendEmail($contato);

        $contatoDao = new ContatoDao();
        $contato = $contatoDao->save($contato);

        Utils::redirect('home');
    } else {
        echo $errors;        
    }
 }

RiggsFolly感谢您对我的问题和您的关注和帮助

我只是修改了execute方法,并按照您的建议放置try/catch,然后就可以了。现在正在工作

private function execute($sql, Contato $contato) {
    $statement = $this->getDb()->prepare($sql);
    $this->executeStatement($statement, $this->getParams($contato));
    if (!$contato->getId()) {
        return $this->findById($this->getDb()->lastInsertId());
    }
    if (!$statement->rowCount()) {
        throw new NotFoundException('Contato with ID "' . $contato->getId() . '" não existe.');
    }
    return $contato;
}
修改后:

private function execute($sql, Contato $contato) {
    try {
        $statement = $this->getDb()->prepare($sql);
        $this->executeStatement($statement, $this->getParams($contato));
        if (!$contato->getId()) {
            return $this->findById($this->getDb()->lastInsertId());
        }
        if (!$statement->rowCount()) {
            throw new NotFoundException('Contato with ID "' . $contato->getId() . '" não existe.');
        }            
    } catch (Exception $ex) {
        $ex->getTraceAsString();
    }
    return $contato;
}

这只是一节课。你如何调用它的方法?不知道你如何连接,那将是有用的!PDO生成
PDOException
而不是
Exception
捕获,这将非常有用
prepare
execute
generate-pdo-exception,但您也没有在这些方法中尝试/捕获任何有用的东西
private function execute($sql, Contato $contato) {
    try {
        $statement = $this->getDb()->prepare($sql);
        $this->executeStatement($statement, $this->getParams($contato));
        if (!$contato->getId()) {
            return $this->findById($this->getDb()->lastInsertId());
        }
        if (!$statement->rowCount()) {
            throw new NotFoundException('Contato with ID "' . $contato->getId() . '" não existe.');
        }            
    } catch (Exception $ex) {
        $ex->getTraceAsString();
    }
    return $contato;
}