vanderbilt-redcap / external-module-framework-docs

This repo holds the documentation for REDCap's External Module Framework
Other
24 stars 11 forks source link

Hook Error: $conn->query not accepting parameters in php8.3 #13

Closed vt03 closed 3 months ago

vt03 commented 3 months ago

Code that was previously working in PHP 7, does not seem to work in PHP 8.2.

Hook Code:

<?php
global $conn;

$data_table = method_exists('\REDCap', 'getDataTable') ? \REDCap::getDataTable($project_id) : "redcap_data"; 

$result = $conn->query('
SELECT
      `record`
FROM 
' . $data_table . '
WHERE
    project_id = ?
AND
    field_name = ?
AND
    value = ?
', [$project_id, 'participant_id', $pid ]);

?>

Error Message:

Fatal error: Uncaught TypeError: mysqli::query(): Argument #2 ($result_mode) must be of type int, array given

Changing to use mysqli's method from

query

to

execute_query

seems to fix the issue.

mmcev106 commented 3 months ago

Using the mysql object directly is actually unsupported. Please use use $module->query() instead.

vt03 commented 3 months ago

Okay, thank you. The 'Documentation for Plugins, Hooks, & External Modules' should be updated as under the FAQ Hooks section it states:

### What is the preferred method in a hook for querying REDCap's database tables? It is preferred that you utilize the MySQLi extension when making calls to the database. It is also acceptable to utilize PEAR DB or PDO (if your web server supports it). Please note that REDCap automatically performs a MySQLi connection, so if you wish, your hook may utilize REDCap's existing database connection link, which is a global variable named $conn. If you do, first remember to add the line global $conn; near the beginning of your hook's code, which brings the database connection link into the scope of the hook function.

mmcev106 commented 2 months ago

Yes, it should. That's not the only thing in those old docs that should be updated... I'd recommend focusing on the much newer EM docs (this repo) until someone has a chance to go through all those old docs that predated EMs.