I am trying to do some sql querying on cvs file in php. I found this
Writing a PHP file to read from CSV and execute SQL Query
but I was unable to change it for the SELECT statement
my attempt:
<?php
$fin = fopen('./file.csv','r') or die('cant open file');
$conn = mysql_connect('localhost', 'name', 'pw');
If (!$conn) {
die ('Could not connect: ' . mysql_error());
}
@mysql_select_db('name') or die ('Unable to select database');
mysql_query("SET NAMES 'utf8'", $conn);
mysql_query("SET CHARACTER SET 'utf8'", $conn);
echo "Connection succeeded <br />\n";
while (($data=fgetcsv($fin,1000,","))!==FALSE) {
$query = "SELECT * FROM file";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
print_r($row);
}
}
fclose($fin);
mysql_close();
?>
lets say I have an file.csv and I want to execute something like this: Select col1,col2 from file.cv where c3='whatever'
OR is there some other, more elegant way how to querying cvs in php?
Thank you!