Error "expects parameter 2 to be resource" when connecting to the bank

-1

I'm trying to make a connection via php to a mysql server but it always has the following error:

  

Warning: mysql_select_db () expects parameter 2 to be resource, null given in /home/u517649386/public_html/conecta.php on line 6

The code used is as follows:

<?php
$banco = new PDO('mysql:host=mysql.hostinger.com.br;u517649386_teste', 'u517649386_teste','senha')or die(mysql_error());
print "Conexão efetuada com sucesso!";


mysql_select_db('u517649386_teste', $con);
print "Conexão e Seleção OK!"; 


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

     $sql = "SELECT * FROM 'pizzas' LIMIT 0, 30 ";

     $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';
      }
}
?>  

The php version of the server is 5.5.35

    
asked by anonymous 14.11.2016 / 23:43

2 answers

0

The $ con variable is null and moreover the native mysql functions do not work with PDO. Try this: '

   <?php

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

if ($serve->connect_errno) {
    printf("Erro: %s\n", $serve->connect_error);
    exit();
}


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

     $SQL = "SELECT * FROM tipos";

     $re = $serve->query($SQL, , MYSQLI_USE_RESULT);

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

          mysqli_free_result($re);
    }

}
?>  
    
14.11.2016 / 23:51
2

There are three ways to make PHP connect with MySQL, the functions (removed from php7) mysql , the PDO and the MySQLi . The question code mixed the PDO with the removed functions.

The simplest solution would be to use MySQL.

$banco = new mysqli_connect('localhost', 'usuario', 'senha', 'banco');

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

     $sql = "SELECT * FROM 'pizzas' LIMIT 0, 30 ";

     $re = mysqli_query($banco, $sql);

     $num = mysqli_num_row($re);

     if($num > 0){
        while($Linha = mysqli_fetch_object($re)){
           echo "{$Linha->Nome} <br />";
        }
      }else{
         echo 'nenhuma pizza cadastrada';
      }
}
    
15.11.2016 / 12:30