"expects Parameter 2 to be Resource" error when connecting to the database

Asked

Viewed 458 times

-1

I am trying to make a connection via php to a mysql server but always presents 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

  • $con should not have a connection vc should use only PDO, functions mysql_* were removed from the newer versions of php

  • Since the 'mysql_*' functions have been removed which I can use instead?

  • 1

    Use PDO or Mysqli functions, what changes basically is that the connection should be passed as first argument in most functions, can look that answer

  • 1

    Follow the examples of the documentation friend, don’t try to do things in your head, learn how it works.

2 answers

2

There are three ways to make PHP connect with Mysql, the functions (removed from php7) mysql, the PDO and the Mysqli. Question code mixed PDO with removed functions.

The simplest solution would be to use Mysqli.

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

0


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

   <?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);
    }

}
?>  
  • Thank you very much for answering me, the error still continues you can check by the following link http://yhsoftware.esy.es/conecta.php

  • 2

    $banco is a PDO object it will not work with the (removed) mysql functions_*

  • What I should do for PHP to work would be to withdraw the $bank ?

  • @Yuriruan use the function mysql_connect()

  • Remove the line with mysql_select_db and use mysqli_connect(): $banco = mysqli_connect("mysql.hostinger.com.br", "u517649386_teste", "senha", "u517649386_teste");

  • @rray "use the function mysql_connect()" oxi, something is missing?

  • 1

    I managed to resolve thanks for the reply of all she helped in the resolution!

  • 1

    That answer is wrong.

Show 3 more comments

Browser other questions tagged

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