How can i import a txt file to a mysql database where each column ends using the & character ? eg.: texthere&othertexthere&anothertexthere&texthere&texthere writing a php script file ?
or:
It can be done using phpmyadmin ? how it is done in phpmyadmin
@NogDogOct 21.2005 — #Does the table already exist, or do you have to create it "on the fly"?
Will each line in the file have the same number of fields, or can it vary; and will those fields always be in the same order on each line?
If yes to all the above, the below should work. If no, then we'll have to be more creative. [code=php] #### THIS IS UNTESTED, SO NO GUARANTEES #### $data = file("data.txt") or die("Could not read data file"); $connx = mysql_connect('yadda', 'yadda', 'yadda') or die("Unable to connect to mysql"); $db = mysql_select_db('database') or die("Could not select DB"); $count = 0; foreach($data as $line) { $fields = explode('&', $line); if(count($fields) == 5) # have the right number of fields { $query = "INSERT INTO table_name (col1,col2,col3,col4,col5)"; $query .= " VALUES ('" implode("','", $fields) . "')"; $result = @myql_query($query); if($result) { $count += mysql_affected_rows($result); } else { echo "<p>QUERY FAILED: $query - " . mysql_error() . "</p>n"; } } else { echo "<p>INVALID DATA LINE: '$line'</p>n"; } } echo "<p>$count rows were inserted into the table.</p>n"; [/code]