I have two tables recordings
and recording_metas
and I want to select some metas and use the foreign keys from those metas to find the corresponding recording. This is what I have so far. $recording_sql
outputs this
SELECT recording FROM recordings
INNER JOIN recording_metas
ON recordings.id = recording_metas.recording_id
WHERE recording_metas.recording_id IN (SELECT recording_id, meta_key, meta_value FROM recording_metas WHERE meta_key=? AND meta_value=?) LIMIT ?
But why do I get the following error?
Fatal error: Uncaught Error: Call to a member function bind_param() on bool
at this line $stmt->bind_param(str_repeat('s', count($mixed)), ...$mixed);
I'm sure that $meta_sql
works because I tested it separately with the LIMIT pointer in there aswell so the pointers are working fine if I use them on $meta_sql
PHP
function retrieveRecordingsByMetaData($connection, $config, $metas, $limit)
{
$where = "";
for ($i = 0; $i < count($metas); $i++) {
$where .= "meta_key=? AND meta_value=? AND ";
}
$where = preg_replace('/ AND $/', '', $where);
$meta_sql = "SELECT recording_id, meta_key, meta_value FROM $config->meta_table WHERE $where";
$recording_sql = "SELECT recording FROM $config->recording_table
INNER JOIN $config->meta_table
ON $config->recording_table.id = $config->meta_table.recording_id
WHERE $config->meta_table.recording_id IN ($meta_sql) LIMIT ?";
echo ($recording_sql);
$stmt = $connection->prepare($recording_sql);
$mixed = associativeToArrayMixed($metas);
array_push($mixed, $limit);
$stmt->bind_param(str_repeat('s', count($mixed)), ...$mixed);
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error . " \r\n";
die();
}
$result = $stmt->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "recording found";
}
} else {
echo "0 results \r\n";
}
}