0

So I've got problem with my PHP code where I'm trying to run two prepared statements which have been 'prepare'd with the same $mysqli object.

        require 'database.php';
        $mysqli = new mysqli($database_host, $database_user, $database_pass, $database_name);

        /*First check if there is already a preference record for this user
          with this particular course.*/
        $query = "SELECT * FROM timetablePrefs WHERE username=? AND courseID=?";
        $stmt = $mysqli->prepare($query);
        foreach ($courseList as $key => $value)
        {
            $stmt->bind_param("ss", $username, $key);
            $stmt->execute();
            if (($stmt->num_rows) === 1)
                continue;
            else
            {
                $query = "INSERT INTO timetablePrefs (username, courseID, hexColour, hidden) ";
                $query .= "VALUES (?, ?, ?, 0)";
                $prepared = $mysqli->prepare($query);
                $prepared->bind_param("sss", $username, $key, "#FFFFFF");
                if (!($prepared->execute()))
                    print("Sorry couldnt change your colour preferences.");
                $prepared->close();
            }
        }
        $stmt->close();
        $mysqli->close();

I may be missing something here; can you even run two of these at the same time? Any help would be greatly appreciated!Any questions please ask ;)

The error I receive is with regards to the line; $prepared->bind_param("sss", $username, $key, "#FFFFFF"); Fatal error: Call to a member function bind_param() on a non-object

Kara
  • 6,115
  • 16
  • 50
  • 57
David S
  • 73
  • 1
  • 10
  • Ahh sorry that would help. The error I get is 'PHP Fatal error: Call to a member function bind_param() on a non-object' in regards to the $prepared->bind_param("sss", $username, $key, "#FFFFFF'); line – David S Apr 27 '14 at 17:38

2 Answers2

1
  1. To get the actual error
  2. To solve it. From the error you can tell that there are results from the previous query pending. Need to store them
Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thanks for getting back so quickly. Don't suppose you have a better explanation of the store_result function... The PHP documentation isn't too detailed as to the why. – David S Apr 27 '14 at 17:50
-2

I was doing my thing the procedural way but also having same kind of issue.So I created two different DB connection variables (just a hack not proper solution) and instead of doing:

stmt_pnl = mysqli_stmt_init($conn);
stmt_bank = mysqli_stmt_init($conn);

I did:

stmt_pnl = mysqli_stmt_init($conn);
stmt_bank = mysqli_stmt_init($conn_bank);

And the error gone and it worked.

Edit: The above answer was just a hack to solve the problem not an actual solution. Proper way to solve this error is to follow below sequence:

  1. Prepare Statement (initialize and bind parameters)
  2. Execute Statement
  3. Get results (mysqli_stmt_get_result)
  4. Close Statement.

(special thanks to Your Common Sense)