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

Asked

Viewed 48 times

1

I need urgent 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’ve broken my head behind a conversion and did not find it very necessary to 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';
      }
}
?>  
  • This question may help you: http://answall.com/questions/47880/comond-update-mycodow-mysql-para-mysqli It’s not difficult!

1 answer

1


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

Your code would look 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);
    }

}
?>  
  • Perfect guy!!! vlw msm only not 100% because it’s still giving it: Warning: mysqli_num_rows(): Fuction cannot be used with Mysql_use_resultin

  • fixed the error now ta 100% just replaced MYSQL_USE_RESULT for $result

  • 1

    Do not need to use as object, can use functional as used the mysql_, just hit the parameters.

Browser other questions tagged

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