0
How do I convert this SESSION below Mysql to PDO.
<?php
if(count($_SESSION['carrinho']) == 0){
echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
}else{
require("conexao.php");
$total = 0;
foreach($_SESSION['carrinho'] as $codigo => $qtd){
$sql = "SELECT * FROM produto WHERE codigo= '$codigo'";
$qr = mysql_query($sql) or die(mysql_error());
$ln = mysql_fetch_assoc($qr);
$titulo = $ln['titulo'];
$preco = number_format($ln['preco'],2,",",".");
$sub = number_format($ln['preco'] * $qtd,2,",",".");
$total += $ln['preco'] * $qtd;
echo '<tr style="font-size:11px; font-weight:bold; color:#000;">
<td align="left">'.$titulo.'</td>
<td align="center"><input type="text" size="1" name="prod['.$codigo.']" value="'.$qtd.'" /></td>
<td align="center">R$ '.$preco.'</td>
<td align="center">R$ '.$sub.'</td>
<td align="center">
<a href="?acao=del&codigo='.$codigo.'">
<img width="25" src="img/del.png" title="Remover '.$titulo.'"/>
</a>
</td>
</tr>';
}
$total = number_format($total,2,",",".");
echo '<tr>
<td align="right" style="font-size:16px; font-weight:bold; color:#990000;" colspan="3">Total</td>
<td align="left" style="font-size:16px; font-weight:bold; color:#000;" colspan="4">R$ '.$total.'</td>
</tr>';
}
?>
For she is returning me this error message:
Deprecated: mysql_query(): The mysql Extension is deprecated and will be Removed in the Future: use mysqli or PDO
I believe it is because these two lines are with mysql_query():
$qr = mysql_query($sql) or die(mysql_error());
$ln = mysql_fetch_assoc($qr);
Friends can give me a help to convert these two lines to PDO?
From the example that Wees gave I would change only sql and add bindValue. $sql="SELECT * FROM product WHERE code= :code", and then add $exec->bindValue('code',$code).
– Alexandre Cardoso
@Alexandrecardoso agree, can be used the
bindValue()
, I believe the references I left say something about.– Wees Smith
Surely you must be there, I just wanted to supplement the answer.
– Alexandre Cardoso
Thanks for the tip, sometimes there is something beaten even kkk
– Wees Smith