Académique Documents
Professionnel Documents
Culture Documents
Introduction
Représente une requête préparée et, une fois exécutée, le jeu de résultats associé.
Synopsis de la classe
/* Propriétés */
readonly string $queryString;
/* Méthodes */
public bindColumn ( mixed $column , mixed &$param [, int $type [, int $maxlen [, mixed
$driverdata ]]] ) : bool
public bindParam ( mixed $parameter , mixed &$variable [, int $data_type =
PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] ) : bool
public bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ]
) : bool
public closeCursor ( void ) : bool
public columnCount ( void ) : int
public debugDumpParams ( void ) : void
public errorCode ( void ) : string
public errorInfo ( void ) : array
public execute ([ array $input_parameters ] ) : bool
public fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [,
int $cursor_offset = 0 ]]] ) : mixed
public fetchAll ([ int $fetch_style [, mixed $fetch_argument [, array $ctor_args =
array() ]]] ) : array
public fetchColumn ([ int $column_number = 0 ] ) : mixed
public fetchObject ([ string $class_name = "stdClass" [, array $ctor_args ]] ) : mixed
public getAttribute ( int $attribute ) : mixed
public getColumnMeta ( int $column ) : array
public nextRowset ( void ) : bool
public rowCount ( void ) : int
public setAttribute ( int $attribute , mixed $value ) : bool
public setFetchMode ( int $mode ) : bool
}
Propriétés
queryString
chaîne de caractères utilisée pour la requête.
Sommaire
<?php
/**
*
*
*
*/
namespace customs;
/**
*
*
*
*/
/**
*
*
*
*/
/**
*
*
*
*/
/**
*
*
*
*/
if (isset($executionError[$i]))
return $executionError[$i];
return $executionError;
}
/**
*
*
*
*/
return $this->executionTime;
}
/**
*
*
*
*/
/**
*
*
*
*/
/**
*
*
*
*/
return true;
}
/**
*
*
*
*/
if ($executed = parent::execute($inputParams))
$this->executionTime = microtime(true) - $this->executionTime;
return $executed;
}
/**
*
*
*
*/
if (!empty($resultSet)) {
$queryString = $this->queryString;
$inputParams = $this->inputParams;
$sth = null;
}
return $resultSet;
}
}
?>
Solved ;)
<?php
$host = "yourHost";
$user = "yourUser";
$pass = "yourPass";
$db = "yourDB";
$cursor = "cr_123456";
try
{
$dbh = new PDO("pgsql:host=$host;port=5432;dbname=$db;user=$user;password=$pass");
echo "Connected<p>";
}
catch (Exception $e)
{
echo "Unable to connect: " . $e->getMessage() ."<p>";
}
$dbh->beginTransaction();
$dbh->query($query);
?>
$datos = array( 'name' => 'Cathy', 'addr' => '9 Dark and Twisty', 'city' => 'Cardiff'
);
$stmt = $pdo->prepare("INSERT INTO colegas (name, addr, city) value (:name, :addr,
:city)");
// o bien
$stmt->execute($datos);
}
catch(PDOException $err)
{
// Mostramos un mensaje genérico de error.
echo "Error: ejecutando consulta SQL.";
}
$stmt = $pdo->prepare('SELECT name, addr, city from colegas where city =:ciudad'); (con
parámetros)
$datos = array( ':ciudad' => 'Santiago');
try{
$stmt->execute($datos);
}
catch(PDOException $err)
{
// Mostramos un mensaje genérico de error.
echo "Error: ejecutando consulta SQL.";
}
2.2.- Leemos los datos del recordset (conjunto de registros) que nos devuelve SELECT en el
objeto PDOStatement.
2.2.1.- Se puede leer cada fila del recordset con ->fetch() del objeto PDOStatement o
mediante ->fetchAll() (obtiene todas las filas del recordset).
'''Este bloque de código lo ejecutaremos dentro de la sección try { ..... }'''
while($row = $stmt->fetch()) {
echo $row['name'] . "<br/>";
echo $row['addr'] . "<br/>";
echo $row['city'] . "<br/>";
}
$row = $sql->fetchAll();
foreach($data as $row)
$id = $row['id'];
$content = $row['content'];
}
Example of persist in bd
//Example of select
//Example of delete
$stmt->execute();
return $stmt->rowCount();
}
Example of persist in bd
//Example of select
//Example of delete
$stmt->execute();
return $stmt->rowCount();
}
There are many references around for returning a refcursor from a pgSQL function using
pg_query. All essentially boil down to executing the following single statement (or some
variation of it):
In PDO, this doesn't work because PDO won't allow multiple statements submitted as a
single statement (due to SQL injection detection). Instead, try this or similar:
<?php
$sql = 'select yourFunction(params...)';
$db = new PDO('pgsql:dbname=yourDBname');
$db->beginTransaction();
$cmd = $db->prepare($sql);
if ($cmd->execute()) {
if ($query = $db->query('fetch all in cursorname')) {
...processing...
$query->closeCursor();
$cmd->closeCursor();
}
}
$db->commit();
?>
add a note
PDO
Introduction
Installation/Configuration
Constantes pré-définies
Connexions et gestionnaire de connexion
Transactions et validation automatique (autocommit)
Requêtes préparées et procédures stockées
Les erreurs et leur gestion
Les gros objets (LOB)
PDO
» PDOStatement
PDOException
Pilotes PDO
Copyright © 2001-2019 The PHP Group My PHP.net Contact Other PHP.net sites