2016-07-15 3 views
0

das Problem: mit PHP anfügen, wie füge ich die Abfrage Daten, die ich aus der Datenbanktabelle in die Konfigurationsdatei an verschiedene spezifische Positionen.mit php wie schreibe ich einen Code, der die Abfrage Daten in eine Datei an verschiedenen Positionen aus der Datenbanktabelle

die Datei, die

$filename = 'C:\wamp\www\disable_user\configurations\config_2'; 


$fp = fopen($filename, 'r+'); 

$sql="SELECT link FROM garden where id ='1' "; 
    if ($result= (mysqli_query($con,$sql))) 
     { 

Fetch ein und eine Reihe

while($row=mysqli_fetch_row($result)) 
       { 

        $replacement = $row[0]; 

        $specific_line = 10; // sample value squeeze it on this line 

        //echo $specific_line; 
        //treat file as an array here. 

        $contents = file('C:\wamp\www\disable_user\configurations\config_2', FILE_IGNORE_NEW_LINES); 

        if($specific_line > sizeof($contents)) 
        { 
         $specific_line = sizeof($contents) + 1; 
        //////echo $specific_line; 
        } 
        // Remove a portion of the array and replace it with something else 

        //array_splice($input , int $offset ,$length, $replacement = array())  how to splice an array 

        //array_splice($contents, $specific_line, 0, array($replacement)); 

        //array_splice($contents, $specific_line-1, -1, array($contents[$specific_line-1].$replacement)); 

        array_splice($contents, $specific_line-1, 1, array($contents[$specific_line-1].",".$replacement.","));//arrays start at zero index 
        $contents = implode("\n", $contents); 
        //echo $contents; 
        //writes a string to a file 
        file_put_contents('C:\wamp\www\disable_user\configurations\config_2', $contents); 
        echo "changes done"; 
      } 

Antwort

0

Zuerst geändert werden muss, um die Abfragedaten greifen müssen und dann eine Datei öffnen und das Anfügen schreiben Funktion. Hier ist eine Code-Hoffnung, die hilft:

<?php 
$connect=("localhost","root",""); 
mysqli_select_db($connect,'database'); 
$query=mysqli_query($connect,"SELECT * FROM table_name WHERE (if needed put condition)"); 
while($row=mysqli_fetch_array($query)){ 
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 
$txt = "'.$row['values'].'\n"; 
fwrite($myfile, $txt); 
$txt = "'.$row['values1'].'\n"; 
append(array($myfile, $txt)); 
fclose($myfile); 
} 
?> 
Verwandte Themen