2016-12-08 7 views
1

나는 PHP 출력 내 SQL 쿼리를 변환하는 PHP 파일이 있습니다. 모든 것이 잘 작동합니다. 하지만 지금 txt.file로 출력이 필요하고 코드가 작동하지 않습니다. 다음은 쿼리PHP로 txt.file로 SQL 결과

는 :

<?php 
$pdo = new PDO('mysql:host=localhost;dbname=DB', 'USER', 'PW'); 

$result = ("SELECT o.order_id AS Bestellnummer, o.customer_id AS Kundennummer, o.order_status_id AS Bestellstatus, op.quantity AS Anzahl, op.model AS Pharmacode, op.name AS Bezeichnung, p.location AS Lieferant, o.shipping_company AS Firma, o.shipping_firstname AS Vorname, o.shipping_lastname AS Nachname, o.shipping_address_1 AS Lieferadresse, o.shipping_postcode AS PLZ, o.shipping_city AS Ort 
FROM oc_order o 
INNER JOIN oc_order_product op 
ON o.order_id = op.order_id 
INNER JOIN oc_product p 
ON op.product_id = p.product_id 
WHERE o.order_status_id = 1 AND p.location = 1 
ORDER BY o.order_id, op.model"); 



echo "<table border='0'> 
<tr> 
<th>Best.Nr</th> 
<th>Kd.Nr</th> 
<th>Status</th> 
<th>Anz.</th> 
<th>Pharmacode</th> 
<th>Bezeichnung</th> 
<th>Lieferant</th> 
<th>Firma</th> 
<th>Vorname</th> 
<th>Nachname</th> 
<th>Adresse</th> 
<th>PLZ</th> 
<th>Ort</th> 
</tr>"; 

foreach ($pdo->query($result) as $row) { 

    echo "<tr>"; 
    echo "<td>" . $row['Bestellnummer'] . "</td>"; 
    echo "<td>" . $row['Kundennummer'] . "</td>"; 
    echo "<td>" . $row['Bestellstatus'] . "</td>"; 
    echo "<td>" . $row['Anzahl'] . "</td>"; 
    echo "<td>" . $row['Pharmacode'] . "</td>"; 
    echo "<td>" . $row['Bezeichnung'] . "</td>"; 
    echo "<td>" . $row['Lieferant'] . "</td>"; 
    echo "<td>" . $row['Firma'] . "</td>"; 
    echo "<td>" . $row['Vorname'] . "</td>"; 
    echo "<td>" . $row['Nachname'] . "</td>"; 
    echo "<td>" . $row['Adresse'] . "</td>"; 
    echo "<td>" . $row['PLZ'] . "</td>"; 
    echo "<td>" . $row['Ort'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 


mysql_close($con); 
?> 

나는 TXT 또는 CSV의 출력에 대한 ENDCODE가 필요합니다. 초기 코드 (SELECT ...) 출력에 대한 새 코드를 연결할 명확한 출력 문자열이 있다고 가정합니다.

+0

마지막 코드가 정확한지 : 그래서 당신이 $string에 각 행을 루프 전에 $string 변수를 생성하고 추가 필요 'PHP'에서 문제가되는'PDO' 대신'mysql_ *'함수를 사용하고 있습니다. –

+0

mysql 확장은 사용되지 않습니다. 'mysqli' 또는'PDO'를 사용하여'SQL Injection'과 같은 취약성 공격으로부터 자신을 구하십시오. – Perumal

+0

@JiriHrazdil mysql 확장은 PHP 7에서 문제가 될뿐만 아니라 PHP 5.5.0부터는 사용되지 않습니다. 5.5.0 이후 버전은 모두 mysql을 사용하여 문제를 일으킨다. – Perumal

답변

0

당신은 PHP에 의해 파일에 넣어보다 전체 문자열을 수집 시도 할 수 있습니다 : 유일한 차이점

$string = ""; 
$file_path = "file.csv"; 
foreach ($pdo->query($result) as $row) { 
    $string .= $row['Bestellnummer'] . ','; 
    ... 
} 
file_put_contents($file_path, $string) 
+0

나는 시도했지만 긍정적 인 효과없이 : file_put_contents ("test.txt", $ output); $ query = $ this-> db -> $ query ("output");이 필요합니다. 이것은 기본 코드에서 작동하지 않습니다. – 27eleven

+0

코드를 표시하십시오. –

+0

안녕하세요, 전체 코드로 대답 할 수 없으므로 초기 질문에 전체 코드를 게시했습니다. – 27eleven