problesmas with mysqli_num_rows and mysqli prepared queries

1

Good afternoon community stack , I'm doing some queries prepared with mysqli the query is successful but if agrgo mysqli_num_rows throws me error.

example this is the code that throws me wrong ...

$me=$_SESSION["id"];
 $name=$_SESSION["usuario"];
 $id =$_GET["id"];

$query3 = "SELECT user_id,post_id FROM post_views WHERE user_id = ?  AND post_id=?";
$result3=mysqli_prepare($conexion,$query3);
$ok3=mysqli_stmt_bind_param($result3, "ii",$me,$id);
$ok3=mysqli_stmt_execute($result3);
if ($ok3==false) {

    echo "error".mysqli_error($conexion);
}else{

   if (mysqli_num_rows($result3)>0) {// si el usuario ya esta no me lo insertes

         echo "not again";
   }else{ //si el usuario no estas insertalo

  $sql="INSERT INTO post_views (user_id,post_id,user_name)VALUES(?,?,?)";

   $result2=mysqli_prepare($conexion,$sql);
   $ok2=mysqli_stmt_bind_param($result2, "iis",$me, $id,$name);
   $ok2=mysqli_stmt_execute($result2);
}

 mysqli_stmt_close($result3);   

}

This is the error that the console shows me:

  

"\ r \ n
\ n Warning : mysqli_num_rows () expects parameter 1 to   be mysqli, object given in    C: \ xampp \ htdocs \ get_info_img_profile.php on line    35
\ n
\ n Warning : mysqli_stmt_bind_param ()   expects parameter 1 to be mysqli_stmt, boolean given in    C: \ xampp \ htdocs \ get_info_img_profile.php on line    43
\ n
\ n Warning : mysqli_stmt_execute () expects   parameter 1 to be mysqli_stmt, boolean given in    C: \ xampp \ htdocs \ get_info_img_profile.php on line    44 \ n {\ "count \": \ "10 \", \ "num_coments \": 0, \ "num_eva \": 1, \ "num_sync \": 3} "

now if I remove the condition of mysqli_num_rows proceed and execute the example code:

 $me=$_SESSION["id"];
 $name=$_SESSION["usuario"];
 $id =mysqli_real_escape_string($conexion, $_GET["id"]);
 $count =$conexion, $_GET["count"];

$query3 = "SELECT user_id,post_id FROM post_views WHERE user_id = ?  AND post_id=?";
$result3=mysqli_prepare($conexion,$query3);
$ok3=mysqli_stmt_bind_param($result3, "ii",$me,$id);
$ok3=mysqli_stmt_execute($result3);
if ($ok3==false) {

    echo "error".mysqli_error($conexion);
}else{


$sql="INSERT INTO post_views (user_id,post_id,user_name)VALUES(?,?,?)";

   $result2=mysqli_prepare($conexion,$sql);
   $ok2=mysqli_stmt_bind_param($result2, "iis",$me, $id,$name);
   $ok2=mysqli_stmt_execute($result2);


 mysqli_stmt_close($result3);   

}

I hope you can help me with this error thanks!

    
asked by andy gibbs 24.08.2018 в 22:43
source

3 answers

1

I was doing tests on your code and I found something very strange.

The PHP manual says that to run num_rows it is necessary First put the result set in the buffer by store_result :

  

The behavior of mysqli_num_rows() depends on whether it is   they use resultsets with or without a buffer. In case of using them without buffer    mysqli_num_rows() will not return the correct number of rows until   all the rows of the result have been recovered.

The funny thing is that with the procedural style I have not been able to make it work and I do not know why (it remains to investigate). However, if we use the object-oriented style, otherwise clearer and more modern, everything works on wheels.

The question is that before using num_rows you must use store_result so that the handler knows how many rows the query brought.

Another option (even more recommended) would be to count the rows with COUNT and read the result of that column.

I propose this code:

$me=$_SESSION["id"];
$name=$_SESSION["usuario"];
$id =$_GET["id"];

$sqlSelect = "SELECT user_id,post_id FROM post_views WHERE user_id = ?  AND post_id=?";
$stmtSelect=$conexion->prepare($sqlSelect);
$stmtSelect->bind_param("ii",$me,$id);
if(!$stmtSelect->execute()){
    echo "error: ".$conexion->error;
}else{
    /*Sin esto no puedes usar num_rows*/
    $stmtSelect->store_result();
    if ($stmtSelect->num_rows>0) {// si el usuario ya esta no me lo insertes
         echo "not again";
    }else{ //si el usuario no estas insertalo
       $sqlInsert="INSERT INTO post_views (user_id,post_id,user_name)VALUES(?,?,?)";
       $stmtInsert=$conexion->prepare($sqlInsert);
       $stmtInsert->bind_param("iis",$me, $id,$name);
       $stmtInsert->execute();
       $stmtInsert->close();
       /*Quizá convenga al menos decir que se insertaron registros*/       
    }
    $stmtSelect->close();      
}
$conexion->close();

I have allowed myself to give more descriptive names to the variables. It's always good to know what you're doing, never variable names in the style: $variable1, $variable2, $variable999999 ... at that point I want to see you when you want to know what the variable $variable999999 does or when another programmer has to review your code or the same be incorporated into teamwork.

    
answered by 25.08.2018 / 07:43
source
0

The variable $result3 is an object and not the result of the query. The correct way would be like this:

if($result3->num_rows > 0) {
    
answered by 24.08.2018 в 22:49
0

As I see the variable result3 is only prepared, therefore the one that the query should bring is $ok3

It should look like this:

if($ok3->num_rows > 0) {
    
answered by 24.08.2018 в 23:29