How would this PHP be converted to mysqli instead of mysql? [duplicate]

1

I need urgently the following PHP that was built in msql is written in mysqli it is part of an application in which I am developing I have no knowledge in PHP I have already broken my head after a conversion and I did not find much help.

The code is as follows

<?php

$serve = mysql_connect('127.0.0.1', 'root', '');
if(!$serve){ echo 'erro';}
$db = mysql_select_db('pizzas', $serve);


if($_GET['acao'] == 'listapizzas'){

     $SQL = "SELECT * FROM tipos";

     $re = mysql_query($SQL, $serve);

     $num = mysql_num_rows($re);

     if($num > 0){

           while($Linha = mysql_fetch_object($re)){
                  echo "{$Linha->Nome}<br />";
           }

      }
      else{
          echo 'nenhuma pizza cadastrada';
      }
}
?>  
    
asked by anonymous 15.11.2016 / 05:05

1 answer

1

mysqli_ * works as an object. Take a look at the manual for you to understand: Manual MySQLI

Your code looks like this:

     <?php

    $serve = mysqli_connect('127.0.0.1', 'root', 'senha','banco'); // Se vc indica o banco aqui não precisa selecionar depois

if($_GET['acao'] == 'listapizzas'){

     $SQL = "SELECT * FROM tipos";

     $result = $serve->query($SQL, $result);

    if(mysqli_num_rows($result)<0){
        printf("Sem registros!");
    } else {
        // Laço
         while ($obj=mysqli_fetch_object($result)){
             printf("%s\n",$obj->nome); 
         }

          mysqli_free_result($result);
    }

}
?>  
    
15.11.2016 / 11:21