1

Mysqlテーブルはこのようなものです

NumberRenamed | CompanyName
---------------------
 1            | a
 2            | aa
 3            | z

$_POST['transaction_partner_name']2 つの値が含まれます。たとえばa、 とz

このような結果が必要です

Array
(
    [1] => a
    [3] => z
)

これを試した

foreach($_POST['transaction_partner_name'] as $i => $post_transaction_partner_name) {
$placeholders .= '?,';
}
$placeholders = (rtrim( ($placeholders), ',' ));

try {
$stmt = $db->prepare("SELECT CompanyName, NumberRenamed 
  FROM 2_1_transactionpartners WHERE CompanyName IN ($placeholders)");
$stmt->execute( ($_POST['transaction_partner_name']) );

$stmt->bindColumn('CompanyName', $company_name);
$stmt->bindColumn('NumberRenamed', $number_renamed);

while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
$result = compact("number_renamed", "company_name");
//$result = $company_name. $number_renamed;
echo '<pre>';
print_r($result);
echo '</pre>';
}

}

しかし、得る

Array
(
[number_renamed] => 1
[company_name] => a
)

またはa1_$result = $company_name. $number_renamed;

これも試した

$arraytable = array();
while ($row = $stmt->fetchAll()) {
$arraytable[$row['NumberRenamed']] = $row['CompanyName'];
echo '<pre>';
print_r($arraytable);
echo '</pre>';
}

しかし得る

 Array
 (
     [] => 
 )

何を修正する必要があるかアドバイスしてください

アップデート

$stmt->bindColumn('NumberRenamed', $number_renamed);

試した

$result = array();
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
$result = compact("number_renamed", "company_name");
$result[$row['number_renamed']] = $row['company_name'];
}
echo '<pre>', print_r($result), '</pre>';

得る

Array
(
    [number_renamed] => 1
    [company_name] => a
    [] => 
)
1

それから試した

$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $result[$row['number_renamed']] = $row['company_name'];
}
echo '<pre>', print_r($result), '</pre>';

そして得る

Array
(
    [] => 
)
1
4

2 に答える 2

11
$stmt = $db->prepare("SELECT NumberRenamed, CompanyName FROM 2_1_transactionpartners WHERE CompanyName IN ($placeholders)");

$array_indexed_by_NumberRenamed = $db_stmt->fetchAll(PDO::FETCH_KEY_PAIR);
于 2013-09-20T08:28:45.283 に答える
3

最初のコードを次のように変更してみてください。

$result = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $result[$row['number_renamed']] = $row['company_name'];
}
echo '<pre>', print_r($result), '</pre>';
于 2013-09-20T06:49:42.113 に答える