我有以下表格
元表
id | recording_id | meta_key | meta_value
记录表
id | recording
recording_idon themeta_table是指向记录表上的记录的外键。
现在我有一个带有元键和值的关联数组$metas,$_GET我想要SELECT匹配所有元键和值的记录。我该怎么做?
这就是我到目前为止所拥有的。如何将数组添加到绑定参数中?我在正确的轨道上吗?
我只是无法解决这个问题。
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);
$sql = "SELECT recording_id
FROM $config->meta_table
WHERE " . $where . "
INNER JOIN $config->recording_table
ON $config->meta_table.id=$config->recording_table.id
LIMIT ?";
$stmt = $connection->prepare($sql);
foreach ($metas as $key => $value) {
$stmt->bind_param("s", $key);
$stmt->bind_param("s", $value);
}
$stmt->bind_param("i", $limit);
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";
//$recording = $row["recording"];
//$hex = bin2hex($recording);
//echo ("response=recording" . $id . "=" . $hex . "\r\n");
}
} else {
echo "0 results \r\n";
}
}