0

I am trying to perform a query which will select questions with question_id values not in the array. I saw that you are supposed to use implode to convert the array into a string, but despite me using it I am still getting an error when doing the query.

Notice: Array to string conversion in C:\xampp\htdocs\Project\test.php on line 4

<?php
    require_once 'includes/config.php';
    $used = implode(',',array(0, 1, 2, 3, 4, 5, 6));    
    $sql = "SELECT * FROM questions WHERE module_id = ? AND (question_id NOT IN ('$used') ORDER BY RAND()"; 
    $stmt = mysqli_prepare($connect, $sql);
    mysqli_stmt_bind_param($stmt, "i", $_SESSION['module_id']);
    mysqli_stmt_execute($stmt);
?>
<form method="POST" action="test.php">

    <button type="submit" name="submit">Submit</button>

</form>

EDIT: Moving the implode function into a seperate variable solved the sql query, however I am now getting these errors:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in C:\xampp\htdocs\Project\testy.php on line 8

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in C:\xampp\htdocs\Project\testy.php on line 9

Looking at the prepared statement, I cant see where it would go wrong?

Zestyy99
  • 287
  • 2
  • 10
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Mar 14 '18 at 17:09
  • 1
    `echo $sql` and it'll hopefully be clear where you've gone wrong. You're not building the query you think you are. – iainn Mar 14 '18 at 17:12
  • SELECT * FROM questions WHERE module_id = ? AND (question_id NOT IN ('.implode(',', Array).')) ORDER BY RAND() – Zestyy99 Mar 14 '18 at 17:14
  • $use= implode(',', $used); $sql = "SELECT * FROM questions WHERE module_id = ? AND (question_id NOT IN ('$use') ORDER BY RAND()"; – pedram shabani Mar 14 '18 at 17:15

1 Answers1

0

this is because you implode function is not being called. its inside quotes. try this it will work just fine

<?php
    require_once 'includes/config.php';
    $used = implode(',',array(0, 1, 2, 3, 4, 5, 6));    
    $sql = "SELECT * FROM questions WHERE module_id = ? AND (question_id NOT IN ($used) ORDER BY RAND()";
    $stmt = mysqli_prepare($connect, $sql);
    mysqli_stmt_bind_param($stmt, "i", $_SESSION['module_id']);
    mysqli_stmt_execute($stmt);
?>
<form method="POST" action="test.php">

    <button type="submit" name="submit">Submit</button>

</form>
Tendai
  • 100
  • 8
  • Thank you that solved the query but now im getting the errors: Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in C:\xampp\htdocs\Project\test.php on line 8 – Zestyy99 Mar 14 '18 at 17:20
  • 1
    Did you check if `mysqli_prepare` actually returns a prepared statement, I assume it returns `false` because some error occured. – ChristianM Mar 14 '18 at 17:32
  • that means there's an error in executing the query. Can you put it in a try and dump the catch excpetion? – Tendai Mar 14 '18 at 17:33