Ciao a tutti ho un problema sull'uso del metodo execute.
Questo il codice:
L'execute mi restituisce false.Codice PHP:$this->pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$query = $this->pdo->prepare('SELECT :fields FROM :table');
$query->bindParam(':fields',$fields,PDO::PARAM_STR);
$query->bindParam(':table',$table,PDO::PARAM_STR);
if ($query->execute()) {
$this->data = $query->fetchAll(PDO::FETCH_OBJ);
var_dump($this->data);
}
Se invece sostituisco un solo parametro...
Ottengo...Codice PHP:$this->pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$query = $this->pdo->prepare('SELECT :fields FROM mytable');
$query->bindParam(':fields',$fields,PDO::PARAM_STR);
if ($query->execute()) {
$this->data = $query->fetchAll(PDO::FETCH_OBJ);
var_dump($this->data);
}
Se non sostituisco niente...codice:array(30) { [0]=> object(stdClass)#4 (1) { ["pagetitle"]=> string(9) "pagetitle" } [1]=> object(stdClass)#5 (1) { ["pagetitle"]=> string(9) "pagetitle" } [2]=> object(stdClass)#6 (1) { ["pagetitle"]=> string(9) "pagetitle" }
Ottengo...Codice PHP:$this->pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$query = $this->pdo->prepare('SELECT field1 FROM mytable');
if ($query->execute()) {
$this->data = $query->fetchAll(PDO::FETCH_OBJ);
var_dump($this->data);
}
Che è quanto vorrei ottenere usando però bindParam come nel primo caso.codice:array(30) { [0]=> object(stdClass)#4 (1) { ["pagetitle"]=> string(8) "Homepage" } [1]=> object(stdClass)#5 (1) { ["pagetitle"]=> string(37) "Integrazione di un plugin per YouTube" } [2]=> object(stdClass)#6 (1) { ["pagetitle"]=> string(30) "La kuBOX al MIA 2009 di Rimini" }
Dove sbaglio?
Grazie.
Simone

Rispondi quotando
, in genere le variabili di un prepared statement si possono solo usare per valori e non per dati tecnici come nomi di campi o colonne.
