Q:

Getting fetch_object() error on a successful query

I'm getting this error:
Fatal error: Call to a member function fetch_object() on a non-object in C:\.....php on line 136

Here's the code:
if ($result = $mysqli->query("INSERT INTO partlistlist ( P_fam , P_code , P_name, P_var , P_ver , P_lnk , P_fol , P_Notes , P_status , P_op ) VALUES ( \"".$mod_fam ."\", \"".$mod_code ."\", \"".$mod_name ."\", \"".$mod_var  ."\", 1 , \"".$mod_lnk  ."\", \"".$mod_fold ."\", \"".$mod_note     ."\", \"".$mod_stat ."\", \"".  $_SESSION['wh_pwd_usr']."\"  ) "));
{
    echo "<br>Articolo creato con successo";
    $created_id = $mysqli->insert_id;
    if (!$result = $mysqli->query("SELECT * FROM partlistlist WHERE P_id = $created_id LIMIT 1"))
        echo "Error";
    $row = $result->fetch_object();
    $P_id = $created_id;

    if (!$result_clonepart = $mysqli->query("SELECT * FROM partlist WHERE K_pid = $old_P_id"))
    {
        echo "Error";
    }
    else
    {
        if ($result_clonepart->num_rows > 0)
        {
            while ($row_clonepart = $result_clonepart->fetch_object())
            {
                if (!$result_clonepart = $mysqli->query("INSERT INTO partlist ( K_ref ,K_pid ,K_iid ,K_qty ,K_subpid, K_stat ) VALUES ( \"".$row_clonepart->K_ref      ."\" ,\"". $P_id    ."\" ,\"".$row_clonepart->K_iid     ."\" ,\"".$row_clonepart->K_qty     ."\" ,\"".$row_clonepart->K_subpid   ."\" ,\"".$row_clonepart->K_stat       ."\")"))
                    echo "Error";
            }

        }
    }
}

Line 136 is:
while ($row_clonepart = $result_clonepart->fetch_object())

How can $result_clonepart not be an object, if the the num_rows check is good?

A:

In your while loop you are reassigning $result_clonepart (what you probably don't want to do). If that query fails your script will stop with a fatal error. 
Use a different variable name (if you don't want to use the same variable name) or break the loop on failure (if you are reassigning intentionally) 
while ($row_clonepart = $result_clonepart->fetch_object()) {
  //     VVVVVVVVVVV     Here is the reassignment
  if (!$result_clonepart = $mysqli->query("INSERT INTO partlist ( K_ref ,K_pid ,K_iid ,K_qty ,K_subpid, K_stat ) VALUES ( \"".$row_clonepart->K_ref      ."\" ,\"". $P_id    ."\" ,\"".$row_clonepart->K_iid     ."\" ,\"".$row_clonepart->K_qty     ."\" ,\"".$row_clonepart->K_subpid   ."\" ,\"".$row_clonepart->K_stat       ."\")"))
      echo "Error";
      //You could insert a break statement if you are reassigning the variable intentionally
}

