PHP准备语句将多个变量绑定到一个数组

时间:2014-04-13 17:00:08

标签: php prepared-statement

我正在尝试将变量绑定到一个数组中。目前,我将它们放在自己的阵列中。我需要做些什么才能将它们绑定到一个数组中?

$catSelect = $db->prepare("SELECT... WHERE jos_categories.alias = ?");
$catSelect->bind_param('s', $category);
$catSelect->execute();
$catSelect->bind_result($contentAlias, $contentCatID, $contentTitle, $contentID);

$contentAliasArray = array();
$contentCatIDArray = array();
$contentTitleArray = array();
$contentIDArrray   = array();

while ($catSelect->fetch()) {
    $contentAliasArray[]=$contentAlias;
    $contentCatIDArray[] = $contentCatID;
    $contentTitleArray[] = $contentTitle;
    $contentIDArrray[] = $contentID;
}

$catSelect->close();

1 个答案:

答案 0 :(得分:0)

$stmt = $db->prepare("SELECT... WHERE jos_categories.alias = ?");
$stmt->bind_param('s', $category);
$stmt->execute();
$res = $stmt->get_result();
$array = $res->fetch_all();

另一种方法是将mysqli交换为PDO

$stmt = $pdo->prepare("SELECT... WHERE jos_categories.alias = ?");
$stmt->execute([$category]);
$array = $res->fetchAll();