0

I've been sitting on the same small problem now for over 10 hours, so it's time to ask stackoverflow! I'm connected to the database but when calling mysqli_stmt_bind_param I get "invalid object or resource".

I have prepared the following procedures that are trowing the issue in PHP languaje. I have commented the code below explaining what I am trying to do.


<?php
include("conexie.php");

$sentencia = mysqli_stmt_init($enlace);

if (mysqli_stmt_prepare($sentencia, "INSERT INTO actividad 
                                        (codalt,actividad) VALUES (?,?)")) 
{
    mysqli_stmt_bind_param($sentencia, "is",$alternativa,$actividad);//link the parameters with the sentence. Insert
    mysqli_stmt_execute($sentencia);//execute the sentence
    mysqli_stmt_close($sentencia);//close the sentence
    $codact=mysqli_insert_id($enlace);//check the latest raw inserted in the database
    if ($codact==0){
        echo "No ha podido copiarse la actividad, verifica que no estaba copiada";
    }else{
        //when success we retrieve the results from the previous actions
        $sentencia = mysqli_stmt_init($enlace);
        if (mysqli_stmt_prepare($sentencia, 'SELECT titulo, observa, lugar 
                                             FROM acto WHERE codact=?')) 
        {
            mysqli_stmt_bind_param($sentencia, 'i', $codact0);//link the parameters with the sentence. Retrieve
            mysqli_stmt_execute($sentencia);// execute the SQL
                mysqli_stmt_bind_result($sentencia,$titulo,$observa,$lugar);//populate the results to the variables
            //Go through the results
            while (mysqli_stmt_fetch($sentencia)){
                $estado=0;
                Echo ''.$codact.''.$titulo.'<>'.$observa.'<>'.$lugar.'<>'.$estado;//verify the variables are different to null
                if (mysqli_connect_errno($enlace)){
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
                $stmt = mysqli_stmt_init($enlace);
                $query =  "INSERT INTO acto 
                            (codact,titulo,observa,lugar,estado) 
                            VALUES (?, ?, ?, ?, ?)";
                mysqli_stmt_prepare($stmt, $query);
                mysqli_stmt_bind_param($stmt, "isssi", 
                    $codact,$titulo,$observa,
                    $lugar,$estado);//This line (761) throws the error.
                if(mysqli_stmt_execute($stmt)){
                    mysqli_close($stmt);//This line (762) throws the error.
                }
            }
            mysqli_stmt_close($sentencia);// Close the method
        }
    }
}
?>

The description of the error is as follows. Including the results of the SQL


1995
Record 1 var 2
Record 1 var 3
Record 1 var 4
0
Warning: mysqli_stmt_bind_param(): invalid object or resource mysqli_stmt in D:\xampp\htdocs\AAAAA\agenda\agenda.php on line 761

Warning: mysqli_stmt_execute(): invalid object or resource mysqli_stmt in D:\xampp\htdocs\AAAAA\agenda\agenda.php on line 762

1995
Record 2 var 2
Record 2 var 3
Record 2 var 4
0
Warning: mysqli_stmt_bind_param(): invalid object or resource mysqli_stmt in D:\xampp\htdocs\AAAAA\agenda\agenda.php on line 761

Warning: mysqli_stmt_execute(): invalid object or resource mysqli_stmt in D:\xampp\htdocs\AAAAA\agenda\agenda.php on line 762
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    It is definitely not necessary to try an squeeze all your code onto the back of a postage stamp! Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 03 '20 at 17:41
  • 2
    Your code is so messy. I strongly recommend to switch to OOP and enable error reporting for MySQLi. – Dharman Jan 03 '20 at 17:49
  • _but when calling mysqli_stmt_bind_param I get "invalid object or resource"._ On which line. Always show us the complete error message – RiggsFolly Jan 03 '20 at 17:55
  • Your prepare may be failing. It looks correct, so there is something else going on. Check for [mysqli errors](http://php.net/manual/en/mysqli.error.php) after your prepare statement to see why it's failing. – aynber Jan 03 '20 at 17:59
  • My guess: the error is "Commands out of sync; you can't run this command now". We expect that when executing a prepared statement before we fetch all of the rows from the resultset of a previously executed statement (i.e. inside the "while fetch" loop.) As a workaround, we can fetch all of the rows and just store in a local array, then do a `free_result`, and then loop through the array and execute SQL statements. Another workaround might just be to do a mysqli `store_result`, then we can do the fetch loop without the error. Just make sure to do a final `free_result`. – spencer7593 Jan 03 '20 at 19:48
  • MySQL docs: https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html mysqli store_result docs https://www.php.net/manual/en/mysqli.store-result.php – spencer7593 Jan 03 '20 at 19:50

0 Answers0