0

I have an SQL statement in PHP as follows:

$sql = "SELECT t_da_edu_questions.ID, t_da_edu_questions.question_part1, t_da_edu_questions.question_part2, t_da_edu_questions.question_part3, t_da_edu_questions.answer, t_da_edu_questions.option2, t_da_edu_questions.option3, t_da_edu_questions.option4, t_da_edu_questions.option5, t_da_edu_questions.Supplerende, t_da_edu_questions.pic_id, t_da_edu_questions.Category_ID, t_da_edu_questions.approved, t_da_edu_questions.NeedAdmin
        FROM t_da_edu_questions
        WHERE (((t_da_edu_questions.Category_ID=?)) AND ((t_da_edu_questions.approved)=1) AND ((t_da_edu_questions.NeedAdmin)=0) AND
        ((t_da_edu_questions.ID) Not In (SELECT t_da_edu_UserAnswers.ID_qst FROM t_da_edu_UserAnswers WHERE ((t_da_edu_UserAnswers.ID_team = ?) AND T_da_edu_UserAnswers.AnsOrBack = 1 ))))
        ORDER BY RAND()
        LIMIT 1";

$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "ii", $cat, $TeamID); //(Line 36 in error)
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

I get the error:

Fatal error: Uncaught Error: mysqli_stmt object is not fully initialized in C:\xampp\htdocs\app\content\db.php:36 Stack trace: #0 C:\xampp\htdocs\app\content\db.php(36): mysqli_stmt_bind_param(Object(mysqli_stmt), 'ii', '6', 304) #1

If I remove the subquery in "Not In" it works fine. I have not been able to find a similar problem.

So, basically, how do i properly add prepared statement to query with sub query?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Emil Olsen
  • 332
  • 1
  • 9
  • 25
  • Consider normalising your schema, – Strawberry Jun 08 '21 at 21:49
  • I don't have a solution, but just a recommendation to check the return values from each call to a mysqli function. In almost all cases, they return **FALSE** if there is any problem, so you should check for this. If it does return FALSE, then examine the specific error with [mysqli_stmt_error()](https://www.php.net/manual/en/mysqli-stmt.error.php) to get more information about the type and message for the error. You can't proceed with debugging until you get the errors. Get into the habit of checking every return value from these functions. – Bill Karwin Jun 08 '21 at 22:41

1 Answers1

1

As usual, you forgot to enable mysqli error reporting. Turn it on and you will see what error is coming from MySQL. To enable it just add this line before making a connection.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

We can't really tell what the real error is until you enable error reporting. However, I can explain what this cryptic error means.

When you instantiate mysqli_stmt object with mysqli_stmt_init(), it is just a plain PHP object without any prepared statement linked to it. You must call mysqli_stmt_prepare() to prepare the statement on the MySQL server. When there is a problem with your SQL, the preparing will fail and the mysqli_stmt object will remain uninitialized. You can't use such an object for anything.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Even though, the real answer is in the linked post, I posted this as an answer and a sign post to the real solution. – Dharman Jun 08 '21 at 22:39
  • Thanks. I did not know this function. That helped a lot. The error was of course a miss spelling in the late hours... – Emil Olsen Jun 09 '21 at 08:57