Newibie: fetch Obj question - data not showing.
| From: | MEM | Date: | Sun, 05 Apr 2009 16:08:19 +0000 |
| Subject: | Newibie: fetch Obj question - data not showing. | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-291093@lists.php.net to get a copy of this message | ||
Hello,
1)
Ok, with mysqli, to fetch data from a database to a select drop down list, we would do something
like this right? :
<?php
$result = $mysqli->query("SELECT id_cliente, nome_cliente FROM cliente");
echo "<select id='listacliente' name='listacliente'>";
while($row = mysqli_fetch_assoc($result)) {
echo "<option
value=".$row['id_cliente'].">".$row['nome_cliente']."</option>";
}
echo "</select>";
?>
2)
What I’m trying to achieve?
The same thing, but using PDO, the prepare/execute methods, and FETCH_OBJ method to fetch data.
3)
Here is what I have done so far:
For simplifying I’m just trying to echo:
$queryh=$conn->prepare('SELECT id_cliente, nome_cliente FROM cliente');
$queryh->execute();
/*trys to access the method fetchObject of the PDOStatement generated by the execute() PDO method,
and save it on $row variable:*/
$row=$queryh->fetchObject();
/*now I'm trying to echo the results. The 'id_cliente' and 'nome_cliente'
are the column names of my database and, if I get it right, the fectchObject() method should allow
me to access those names as anonymous properties. So: */
echo "Id: ".$row->id_cliente." - Nome: ".$row->nome_cliente."</
br>";
4)
Here is the issue that I’m getting:
I’m getting no values from the database upon echo request.
5)
Question:
What is wrong with this code? :(
Regards,
Márcio