I am currently migrating a PHP project from using PHP's mssql
library to using the PDO
library with FreeTDS. I have modified all of my database calls, but have run into a very strange problem executing the prepared statements on the SQL Server database.
Below is a snippet of code that I am having the problem with.
public static function gift_cards_exist($start_number, $end_number = null) {
global $sol_donations;
$db = $sol_donations->get_db();
$store_id = get_option('pos-store-id', false);
//$store_id = '01'; // Won't work if value assigned this way
$payment_method = 'Gift';
if (empty($end_number)) {
$end_number = $start_number;
}
$query = '
SELECT * FROM dbo.tblPmtSerialHdr
WHERE SerialId BETWEEN ? AND ?
AND StoreId = ?';
//AND PmtMethodId = ?';
$stmt = $db->prepare($query);
$stmt->bindParam(1, $start_number, \PDO::PARAM_STR | \PDO::PARAM_INPUT_OUTPUT);
$stmt->bindParam(2, $end_number, \PDO::PARAM_STR | \PDO::PARAM_INPUT_OUTPUT);
$stmt->bindParam(3, $store_id, \PDO::PARAM_STR | \PDO::PARAM_INPUT_OUTPUT);
//$stmt->bindParam(4, $payment_method, \PDO::PARAM_STR | \PDO::PARAM_INPUT_OUTPUT);
$result = $stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor();
/* Nothing printed from this
echo $stmt->errorCode();
echo '<br />';
echo $stmt->errorInfo();
*/
var_dump($result);
exit();
if ($result) {
return true;
}
return false;
}
Here is where the problem exists. Running the code in this form (with all of the commented lines remaining commented) results in correct execution of the query with the expected, although not correct for my program, results.
However, if I modified the query to use the second AND
statement and uncommented the last bindParam
statement to use with the statement, the script will fail causing a segmentation fault. The actual error message that I get in the apache error log is:
[Fri Feb 06 10:43:05.406772 2015] [core:notice] [pid 1415] AH00051: child pid 1541 exit signal Segmentation fault (11), possible coredump in /etc/apache2
I have absolutely no idea why adding this one extra parameter for the prepared statement is causing a problem.
Some extra information about the situation:
FreeTDS is installed correctly and can be used to connect to the database and query the data.
I have tried using both bindParam
and bindValue
and both give the same results.
I have tried running the statement with and without the bitwise or with PDO::PARAM_INPUT_OUTPUT
.
Finally, I checked to make sure that there are no NULL
in the record set that should be returned to make sure that NULL
values couldn't be the cause.