BigMac66 BigMac66 - 1 year ago 89
MySQL Question

MySQL prepared statement generating internal server error

I am using PHP and MySQL. I have the following PHP code:

$stmt = $this->db_connect()->prepare(
"SELECT P.`pool_id`, P.`pool_name`, PL.`player_id`, PL.`alias`, PP.`paid` "
."FROM `pool` AS P, `player_pool` AS PP, `players` AS PL "
."WHERE P.`sponsor` = ? "
."AND P.`pool_id` = PP.`pool_id` "
."AND PP.`player_id` = PL.`player_id` "
."ORDER BY P.`pool_name`, PL.`alias`;");

echo "validated_user_id=".$_SESSION['validated_user_id'].PHP_EOL;
echo "stmt=".($stmt == null);
$stmt->bind_param("i", $_SESSION['validated_user_id']);

It continually returns 500 Internal Server Error. The logs show [23-Sep-2016 00:05:06] PHP Fatal error: Call to a member function bind_param() on a non-object. The validated_user_id has a valid value but the $stmt variable is null.

At first I thought I was using a reserved word so I escaped everything - no success. I have many other DB queries that look very similar that work just fine...

What really burns me is that the exact same code works fine on my local workspace - this error only appears on the host. Thing is I haven't a clue what to look for to find out why - please help.

Found the problem - there was a very slight difference in the name of one of the columns that was not on the host.

Answer Source

Your prepare() function seems to return null. This might happen due to an error in SQL, but most probably, if it works on your local machine, there is a problem with the database connection.

Check your $this->db_connect() function if it reports connection errors. Separating the db_connect() call from the prepare() call might help to isolate the root cause:

$connection = $this->db_connect();
$stmt = $connection->prepare(...);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download