Articles

old dbtechnique to new PDO technique need help print array

December 13, 2012 at 10:01:14
Specs: Windows 7

I have this website which used the old technique for mysql, the `mysql_query(Select etc.)` way, now i HAVE to change this to the new technique like `$dbh->prepare(SELECT etc.).`

So i used to have this code:
`$sql=("SELECT * FROM product WHERE productcode = $productcode");`
My database table product looks like this
Productcode, categorycode, name,price, etc. this would give output like
01,02,Eco-bubble Wasmachine, $300, etc.

`$product=mysql_query($sql);
$productrow=mysql_fetch_array($product);`

So now i could print the array like this
`'print $productrow["name"];`

But now i use the new technique like this`
$productquery = $dbh->prepare("SELECT * FROM product WHERE productcode = :productcode");
$productquery->bindValue(":productcode", $productcode, PDO::PARAM_INT);
$productquery->execute();
$productrow=$productquery->fetchall(PDO::FETCH_ASSOC);`

But now i can't print the array like
`"print $productrow["name"];"`
I can only print it if I use a `foreachcode` like
`foreach($productrow as $product){
print $product["name"];
}`

So is there a way to do it without the `foreach`?
Because otherwise i have to edit my code and open an `foreach{` everytime i use this in my code, which will be a loooot of work.
How come `print $productrow["naam"];` doesn't work in the new way?
And how can i do this?

Because i use many things like
`<div class="productheader">
<?php print $productrow["naam"];?>
</div>`

So in this new way i would have to do:
`<div class="Productheader">
<?php foreach($productrow as $product){
print $product["naam"];
}
?>`

So, I hope my question is clear, sorry if i have used unclear English, I hope someone can help me! Thanks in advance!


See More: old dbtechnique to new PDO technique need help print array

Reply ↓  Report •

Related Solutions


Ask Question