Conversion of SESSION Mysql to SESSION into PDO

Asked

Viewed 122 times

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?

1 answer

1

First you create the connection to the database:

$dbname="nome_do_banco_de_dados";
$dbuser="nome_do_usuario_do_banco";
$dbpassword="senha_do_usuario";

try{
    $conn=new PDO('mysql:host=localhost;dbname='.$dbname, $dbuser, $dbpassword,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8"));
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $error){
    return 'Erro: '.$error->getMessage();
}

Then run the querys using the PDO:

$sql="SELECT * FROM produto WHERE codigo= '$codigo'";
$exec=$conn->prepare($sql);
$exec->execute();
$results=$exec->fetchAll(PDO::FETCH_ASSOC);

Article on CRUD with PDO - Devmidia

Article on CRUD with PDO - Gigasystems

  • 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).

  • @Alexandrecardoso agree, can be used the bindValue(), I believe the references I left say something about.

  • Surely you must be there, I just wanted to supplement the answer.

  • Thanks for the tip, sometimes there is something beaten even kkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.