PDO在上传未执行的文件时在while循环内选择查询

时间:2014-01-08 22:29:59

标签: php pdo

我正在运行这个PHP PDO代码来上传文件,然后在while循环中运行一个选择查询正在PDO:

if(is_uploaded_file($_FILES['dd_submission_form']['tmp_name'])) {
        echo "<h3>" . "File ". $_FILES['dd_submission_form']['name'] ." uploaded successfully." . "</h3>";
    }

    //Import uploaded file to Database
    $handle = fopen($_FILES['dd_submission_form']['tmp_name'], "r");

    fgetcsv($handle);

    while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if($data[5] == '17') {

            echo '<strong>DD Ref: </strong>'.$data[0].'<br>';

            $stmt = $pdo_conn->prepare("SELECT * from customer where directdebit_reference = :directdebit_reference ");
            $stmt->execute(array(':directdebit_reference' => $data[0]));
            $customer = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if(count($customer) > 0) {

                echo '<h3>'.$customer["company"].'</h3>';

            }
        }
    }

但它似乎没有在while循环中选择任何行

我知道行存在,因为我在PHPMyAdmin中运行查询时会返回行

如果我运行这个MySQL PHP查询:

$sql="SELECT * from customer where directdebit_reference = '".$data[0]."' ";
rs=mysql_query($sql,$conn);
$result=mysql_fetch_array($rs);
echo $result["company"];

它与公司专栏相呼应

1 个答案:

答案 0 :(得分:1)

您正在使用fetchAll方法。它会将查询的所有获取结果作为嵌套数组加载。因此,对于第一个客户的数据,您需要:

echo '<h3>'.$customer[0]["company"].'</h3>';

或者,您可以更新为:

$customer = $stmt->fetch(PDO::FETCH_ASSOC);

或者,更好的是,只需将结果限制为一个。

$stmt = $pdo_conn->prepare("SELECT *
    FROM customer 
    WHERE directdebit_reference = :directdebit_reference
    LIMIT 1");
$stmt->execute(array(':directdebit_reference' => $data[0]));
$customer = $stmt->fetch(PDO::FETCH_ASSOC);