0: INSERT into questions ('examid', 'question', 'type', 'flag_active') VALUES ('33','this is the first question','4','1')
1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''examid', 'question', 'type', 'flag_active') VALUES ('33','this is the first que' at line 1
INSERT into questions ('examid', 'question', 'type', 'flag_active') VALUES ('33','this is the second question','4','1')
1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''examid', 'question', 'type', 'flag_active') VALUES ('33','this is the second qu' at line 1
INSERT into questions ('examid', 'question', 'type', 'flag_active') VALUES ('33','this is the third question','4','1')
1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''examid', 'question', 'type', 'flag_active') VALUES ('33','this is the third que' at line 1
INSERT into questions ('examid', 'question', 'type', 'flag_active') VALUES ('33','this is the fourth question','4','1')
1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''examid', 'question', 'type', 'flag_active') VALUES ('33','this is the fourth qu' at line 1
INSERT into questions ('examid', 'question', 'type', 'flag_active') VALUES ('','','','')
1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''examid', 'question', 'type', 'flag_active') VALUES ('','','','')' at line 1
INSERT into questions ('examid', 'question', 'type', 'flag_active') VALUES ('','','','')
1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''examid', 'question', 'type', 'flag_active') VALUES ('','','','')' at line 1
INSERT into questions ('examid', 'question', 'type', 'flag_active') VALUES ('','','','')
1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''examid', 'question', 'type', 'flag_active') VALUES ('','','','')' at line 1
33 this is the first question 4 1
33 this is the second question 4 1
33 this is the third question 4 1
33 this is the fourth question 4 1
$readfile=file($uploadPath.$name);
for($k=0; $k<=count($readfile); $k++)
{
$fields=explode("\r",$readfile[$k]);
for($i=0; $i<=count($fields); $i++)
{
$values=explode("\t",$fields[$i]);
$columns = "'examid', 'question', 'type', 'flag_active'";
$query=("INSERT into questions ($columns) VALUES ('$values[0]','$values[1]','$values[2]','$values[3]')");
echo $query;
mysql_query($query) ;
echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
}
}
ASKER
$input = file('import.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$filedata= array();
foreach ($input as $line) {
$filedata[] = explode("\t",$line);
}
$columns = "examid, question, type, flag_active";
ASKER
$input = file('import.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$filedata= array();
foreach ($input as $line) {
$filedata[] = explode("\t",$line);
}
var_dump($filedata);
And let me know what you get.ASKER
0: array(1) { [0]=> array(13) { [0]=> string(2) "33" [1]=> string(26) "this is the first question"
[2]=> string(1) "4" [3]=> string(4) "1 33" [4]=> string(27) "this is the second question" [5]=>
string(1) "4" [6]=> string(4) "1 33" [7]=> string(26) "this is the third question" [8]=> string(1)
"4" [9]=> string(4) "1 33" [10]=> string(27) "this is the fourth question" [11]=> string(1) "4"
[12]=> string(1) "1" } }
$input = file('import.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$filedata= array();
foreach ($input as $line) {
$filedata[] = explode("\t",$line);
}
$columns = "examid,question,type, flag_active";
foreach ($filedata as $line) {
$query=("INSERT into questions ($columns) VALUES ('$line[0]','$line[1]','$line[2]','$line[3]')");
echo $query . "\n";
mysql_query($query) ;
echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
}
ASKER
$input=file($uploadPath.$name,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$filedata= array();
foreach ($input as $line) {
$filedata[] = explode("\t",$line);
}
$columns = "examid, question, type, flag_active";
foreach ($filedata as $line) {
$query=("INSERT into questions ($columns) VALUES ('$line[0]','$line[1]','$line[2]','$line[3]')");
echo $query . "\n";
mysql_query($query) ;
echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
}
0: INSERT into questions (examid, question, type, flag_active) VALUES ('33','this is the first question','4','1 33') 0:
$input = file('data.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$filedata=array();
foreach ($input as $line) {
$filedata = explode("\t",$line);
var_dump($filedata);
}
Once that's working, we can move onto the database bits (PDO or mySQLi)
Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
<?php // RAY_fgetcsv.php
error_reporting(E_ALL);
// SHOW HOW TO READ A CSV FILE
// NAME OF THE CSV FILE
$csv = 'RAY_sample_csv.csv';
// OPEN THE FILE FOR READING
$fpo = fopen($csv, 'r');
if (!$fpo) die("CANNOT OPEN $csv");
// READ THE ROWS
while (!feof($fpo))
{
$arr = fgetcsv($fpo);
var_dump($arr);
// CONSTRUCT AND RUN YOUR INSERT QUERY HERE
}
HTH, ~Ray
ASKER
0: array(1) { [0]=> array(13) { [0]=> string(2) "33" [1]=> string(26) "this is the first question"
[2]=> string(1) "4" [3]=> string(4) "1 33" [4]=> string(27) "this is the second question" [5]=>
string(1) "4" [6]=> string(4) "1 33" [7]=> string(26) "this is the third question" [8]=> string(1)
"4" [9]=> string(4) "1 33" [10]=> string(27) "this is the fourth question" [11]=> string(1) "4"
[12]=> string(1) "1" } }
Which is what I got last time I ran it (like above).$input=file($uploadPath.$name,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$filedata=array();
foreach ($input as $line) {
$filedata = explode("\r",$line);
foreach ($filedata as $thing) {
$value = explode("\t",$thing);
var_dump($value);
}
}
0: array(4) { [0]=> string(2) "33" [1]=> string(26) "this is the first question" [2]=> string(1) "4" [3]=> string(1) "1" }
array(4) { [0]=> string(2) "33" [1]=> string(27) "this is the second question" [2]=> string(1) "4" [3]=> string(1) "1" }
array(4) { [0]=> string(2) "33" [1]=> string(26) "this is the third question" [2]=> string(1) "4" [3]=> string(1) "1" }
array(4) { [0]=> string(2) "33" [1]=> string(27) "this is the fourth question" [2]=> string(1) "4" [3]=> string(1) "1" }
ASKER
<?php // RAY_temp_rrattie.php
error_reporting(E_ALL);
echo '<pre>';
// SHOW HOW TO READ A CSV FILE
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28236830.html#a39484705
// NAME OF THE CSV FILE
$csv = 'http://filedb.experts-exchange.com/incoming/2013/09_w37/676827/testfile.txt';
// COPY AND REPAIR THE FILE
$txt = file_get_contents($csv);
$arr = explode("\r", $txt);
$txt = implode(PHP_EOL, $arr);
file_put_contents('RAY_temp_rrattie.csv', $txt);
// OPEN THE REPAIRED CSV
$fpo = fopen('RAY_temp_rrattie.csv', 'r');
if (!$fpo) die("CANNOT OPEN 'RAY_temp_rrattie.csv'");
// READ THE ROWS
while (!feof($fpo))
{
$arr = fgetcsv($fpo,0,"\t");
var_dump($arr);
// CONSTRUCT THE QUERY HERE
}
ASKER
<?php
//sort out line endings
ini_set('auto_detect_line_endings',true);
//connect to your database
$dbUser = "username";
$dbPass = "password";
$conn = new PDO('mysql:host=localhost;dbname=yourDB', $dbUser, $dbPass);
//read the file into an array
$input = file('yourFile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
//prepare the SQL statement
$query = $conn->prepare("INSERT INTO questions (examid, question, type, flag_active) VALUES (?,?,?,?)");
//loop through the array and insert the database records
$filedata=array();
foreach ($input as $line) {
$filedata = explode("\t",$line);
$query->execute($filedata);
}
?>
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY