Ton bloc try...catch ne va jamais lancer d'exception, parce qu'il n'en interceptera jamais. Les fonction PDO::exec() et PDOStatement::execute() renvoient uniquement un booléen et ne lancent pas d'exception.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPUBLIC FUNCTION queryObjectArray() { try { $this->stmt->execute(); while ($result = $this->stmt->fetch(PDO::FETCH_OBJ)) { $resultsObjectArray[] = $result; } if (isset($resultsObjectArray)) { return $resultsObjectArray; } } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } }
$db->select('*', 'rubriques', 'id_rubrique > ?'); $db->bindValues(array('8')); $rows = $db->queryObjectArray(); $nb=count($rows);
for ($i=0;$i<$nb;$i++){ echo $i." - Test ".$rows['id_rubrique']." "; }
try { $this->stmt->execute(); $resultsObjectArray = array(); while ($result = $this->stmt->fetch(PDO::FETCH_OBJ)) { $resultsObjectArray[] = $result; } if (isset($resultsObjectArray)) { return $resultsObjectArray; } }
<?php $arr = array('a','b','c','d','e'); foreach ($arr as $letter) { echo $letter."\n"; } ?>
a b c d e
<?php $arr = array(1=>'a', 2=>'b', 3=>'c', 4=>'d', 5=>'e'); foreach ($arr as $key => $letter) { echo $key." : ".$letter."\n"; } ?>
1 : a 2 : b 3 : c 4 : d 5 : e
return $this -> stmt;
public function queryObjectArray() { try { $this -> stmt -> execute(); $this -> stmt -> setFetchMode(PDO::FETCH_OBJ); return clone $this -> stmt; } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } }
<?php foreach ($rubriques as $k => $rub) { echo $k.' - Test '.$rub->id_rubrique.' '; } ?>
class Crud extends SPDO{ private $conn; private $stmt; /* @desc Connect to database @access public @param string $host @param string $dbname @param string $user @param string $password @return void */ PUBLIC FUNCTION __construct() { try { $this->connect(); } catch (Exception $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Connect to database @access public @param string $host @param string $dbname @param string $user @param string $password @return void */ PUBLIC FUNCTION connect() { try { $this->conn = SPDO::getInstance(); } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Prepare INSERT statement for execution @access public @param string $table @param string $columns @param string $values @return void */ PUBLIC FUNCTION insert($table, $columns, $values) { try { $sql = 'INSERT INTO ' . $table . ' (' . $columns . ') VALUES (' . $values . ')'; $this->stmt = $this->conn->prepare($sql); } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Prepare UPDATE statement for execution @access public @param string $table @param string $set @param string $where @return void */ PUBLIC FUNCTION update($table, $set, $where = null) { try { $sql = 'UPDATE ' . $table . ' SET ' . $set; if (isset($where)) { $sql .= ' WHERE ' . $where; } $this->stmt = $this->conn->prepare($sql); } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Prepare DELETE statement for execution @access public @param string $table @param string $where @param string $orderBy @param int $limit @return void */ PUBLIC FUNCTION delete($table, $where null, $orderBy null, $limit = null) { try { $sql = 'DELETE FROM ' . $table; if (isset($where)) { $sql .= ' WHERE ' . $where; } if (isset($orderBy) AND isset($limit)) { $sql .= ' ORDER BY ' . $orderBy . ' LIMIT ' . $limit; } $this->stmt = $this->conn->prepare($sql); } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Prepare SELECT statement for execution @access public @param string $columns @param string $table @param string $where @param string $aditional @return void */ PUBLIC FUNCTION select($columns, $table, $where null, $additional null) { try { $sql = 'SELECT ' . $columns . ' FROM ' . $table; if (isset($where)) { $sql .= ' WHERE ' . $where; } if (isset($additional)) { $sql .= ' ' . $additional; } $this->stmt = $this->conn->prepare($sql); /* affiche requête préparé*/ echo "requete preparee : ".$sql.' '; } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Bind values to parameters @access public @param array $values @return void */ PUBLIC FUNCTION bindValues($values = array()) { try { $i = 1; foreach ($values as $value) { if (is_numeric($value)) { $dataType = PDO::PARAM_INT; } else { $dataType = PDO::PARAM_STR; } $this->stmt->bindValue($i, $value, $dataType); //echo " ".$value." "; $i++; } } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Execute prepared INSERT, DELETE or UPDATE statement @access public @return int Number of affected rows */ PUBLIC FUNCTION execute() { try { return $this->stmt->execute(); } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Get last inserted row id @access public @return int */ PUBLIC FUNCTION getLastInsertId() { try { return $this->conn->lastInsertId(); } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Execute prepared SELECT statement and query single item @access public @return mixed */ PUBLIC FUNCTION querySingleItem() { try { $this->stmt->execute(); if ($result = $this->stmt->fetch(PDO::FETCH_NUM)) { if (isset($result)) { return $result[0]; } } } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @desc Execute prepared SELECT statement and query single object @access public @return object */ PUBLIC FUNCTION querySingleObject() { try { $this->stmt->execute(); $resultObject = $this->stmt->fetch(PDO::FETCH_OBJ); if (isset($resultObject)) { return $resultObject; } } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @Execute prepared SELECT statement and query object array of rows @access public @return object array */ PUBLIC FUNCTION queryObjectArray() { try { $this->stmt->execute(); $resultsObjectArray = array(); while ($result = $this->stmt->fetch(PDO::FETCH_OBJ)) { $resultsObjectArray[] = $result; } if (isset($resultsObjectArray)) { return $resultsObjectArray; } } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } /* @Compter les lignes dans le tableau (WHERE condition possible) @param string $table @param string $where @param array $values @return int */ PUBLIC FUNCTION countRows($table, $where null, $values array()) { try { $this->select('COUNT(*)', $table, $where); //echo " ".$this->select('COUNT(*)', $table, $where)." "; if (isset($where) AND isset($values)) { $this->bindvalues($values); } $this->stmt->execute(); return $this->stmt->fetchColumn(); } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } } }
public function queryObjectArray() { try { $this -> stmt -> execute(); $this -> stmt -> setFetchMode(PDO::FETCH_OBJ); return clone $this -> stmt; } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } }
<?php foreach ($rubriques as $k => $rub) { echo $k.' - Test '.$rub->id_rubrique.' '; } ?>
PUBLIC FUNCTION queryObjectArray() { try { $this->stmt->execute(); $resultsObjectArray = array(); while ($result = $this->stmt->fetch(PDO::FETCH_OBJ)) { $resultsObjectArray[] = $result; } if (isset($resultsObjectArray)) { return $resultsObjectArray; } } catch (PDOException $e) { printf("Exception: %s \n", $e->getMessage()); exit(); } }
<?php foreach ($rubriques as $k => $rub) { echo $k.' - Test '.$rub->id_rubrique.' '; } ?>