ERROR mysql_num_rows () expects parameter 1 to be resource, boolean given

1

I have an error in my code:

$conexion = connect::con();

$compro_cod = "SELECT * FROM producte WHERE codprod = '$codprod'";
$existe = mysql_query($conexion,$compro_cod);

  if (mysql_num_rows($existe) > 0){

   print_r('Error: Ya existe ese codigo <br/> <a href="index.php?page=controller_accesori&op=list">Volver</a>');

   die();
} else {
    $sql = " INSERT INTO producte (id, accesori, imatge_accesori, email, codprod, marca, modelo, cantitat, data1, data2, pais, idioma, observaciones, valoracio)"
      . " VALUES ('$id','$accesori', '$imatge_accesori', '$email', '$codprod', '$marca', '$modelo', '$cantitat', '$data1', '$data2', '$pais', '$idioma', '$observaciones', '$valoracio')";

$res = mysqli_query($conexion, $sql);
connect::close($conexion);
return $res;

What I want to do is to look in databases if you have that code, and if so, show me the print_r, but I get an error in the "mysql_num_rows" that says:

  

mysql_num_rows () expects parameter 1 to be resource, boolean given

    
asked by Carloscr99 28.12.2017 в 18:17
source

1 answer

2

At the request of @KennyBarrera I will specify my answer better:

In your code you are mixing connection and query instructions from mysql with those from mysqli. That is why your code throws error. You must choose only one. I commented that the instructions of Mysql are obsolete since the version of PHP 5.5.0 and from version 7.0.0 are deleted. More information on this can be seen here

In view of the idea that we are in the world of technology is to innovate, improve and optimize everything that can be my solution will be oriented to mysqli (if you choose mysql then just replace where you see mysqli by mysql and ready !)

Now I leave the code completely corrected, I hope it can be helpful:

$mysqli = new mysqli("localhost", "usuario", "contraseña", "nombre_bd");

$resultado = $mysqli->query("SELECT * FROM producte WHERE codprod = '$codprod'");


  if ($resultado->num_rows > 0){

   print_r('Error: Ya existe ese codigo <br/> <a href="index.php?page=controller_accesori&op=list">Volver</a>');

} else {

$res = $mysqli->query("INSERT INTO producte (id, accesori, imatge_accesori, email, codprod, marca, modelo, cantitat, data1, data2, pais, idioma, observaciones, valoracio)"
      . " VALUES ('$id','$accesori', '$imatge_accesori', '$email', '$codprod', '$marca', '$modelo', '$cantitat', '$data1', '$data2', '$pais', '$idioma', '$observaciones', '$valoracio')");

$mysqli->close();
return $res;
    
answered by 28.12.2017 / 22:12
source