PHP, mysqli return null on function fetch()

Am trying to return a result using mysqli prepared statement but it returns nothing and I don’t know what is wrong with the code.

These are codes I wrote.

> <?php
> require_once ("db/db_connect.php");
> $mysqliDebug = true;
> #mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

>             if($_SERVER["REQUEST_METHOD"] == 'POST'){
>                 if (!empty($_POST['search_field']) && is_numeric($_POST['search_field'])) {
>                     $search_num = trim(filter_input(INPUT_POST, 'search_field', FILTER_VALIDATE_INT));
>                     
>                     //validation
>                     $bad =  false;
>                     
>                     if($search_num < strlen(11) || $serach_num > strlen(11)){
>                         $bad  =  true;
>                         $errors = ['It must be 11 numbers in lenght.'];
>                     }//end of $search_num validation
>                     
>                     #check if $bad is not true
>                     if(!$bad) {
>                         $check_num = "SELECT j_id, j_phone, j_fullname, amount_invested, j_activated FROM j_members WHERE j_phone = ? LIMIT 1";
>                         
>                         if($stmt = $conn->prepare($check_num)){
>                             $stmt->bind_param('i',$search_num);
>                             $stmt->execute();
>                             $stmt->store_result();
>                             $num_of_rows = $stmt->num_rows;
>                             $stmt->bind_result($member_id, $phone_num, $fullnames, $amount_inves, $status);
>                             
>                             while ($stmt->fetch()) {
>                                 echo $member_id.'<br>';
>                                 echo $phone_num.'<br>';
>                                 echo $fullnames.'<br>';
>                             }
>                             
>                         }
>                         
>                         /*$if_available_num = $conn->prepare($check_num);
>                         $if_available_num->bind_param('i', $search_num);
>                         $if_available_num->execute();
>                          $if_available_num->store_result();
>                         $if_available_num->bind_result($member_id, $phone_num, $fullnames, $amount_inves, $status);*/
>                         
>                 
>                     }else{
>                         echo 'Not possible'.'<br>';
>                         echo $conn->error;
>                         echo $conn->connect_error;
>                         #mysqli_error($conn->conn);
>                         #die(mysqli_error($this->conn));
>                     }
>                     while ($if_available_num->fetch()){
>                             echo $member_id.'<br>';
>                             echo $phone_num.'<br>';
>                             echo $fullnames.'<br>';
>                         }
>                 }//end !empty($_POST['search_field'])
>                 
>             }//end of $_SERVER METHOD
>             
>         ?>
> <!DOCTYPE html>
> <html lang="en">
>     <head>
>         <meta charset="utf-8">
>         <title>Activate user</title>
>         <meta name="viewport" content="width=device-width, initial-scale=1.0">
>     </head>
>     <body>
>         
>         <form accept-charset="utf-8" action="" method="post">
>             <fieldset>
>                 <legend>
>                     Search to activate user
>                     <ol>
>                         <li>
>                             <label for="search_field">Search</label>
>                             <input type="text" id="search_field" name="search_field" value="" placeholder="Enter the mobile num you want to activate" required>
>                         </li>
>                         <li>
>                             <input type="submit" id="search" name="search" value="Search">
>                         </li>
>                     </ol>
>                 </legend>
>             </fieldset>
>         </form><?php
>         while ($if_available_num->fetch()){
>                     echo $member_id.'<br>';
>                     echo $phone_num.'<br>';
>                     echo $fullnames.'<br>';
>                 }
>                ?> 
>     </body>
> </html>

which part of the code are you talking about? e.g. the last PHP block will obviously fail on every GET request.

Will this work?

  if($search_num < strlen(11) || $serach_num > strlen(11)){

Apart from the typo, surely the use of strlen() here is incorrect?

This part is in twice:

while ($if_available_num->fetch()){
>                             echo $member_id.'<br>';
>                             echo $phone_num.'<br>';
>                             echo $fullnames.'<br>';
>                         }

but you’ve commented out the query that applies to it, so this may be causing errors too.

But as @Dormilich said above, how far through the code does it get before it goes wrong?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.