0

I do Hope someone can help, I have a table with 7 columns ( All Empty )

id | unqid | Numbers | Date/Time | IP | UserAgent | Confirmed

Basically what im trying to do in php is browse for my CSV File with a single column of numbers, Then upload and isert the numbers from the CSV into The Numbers Column in mysql Table.

I have done this with php by simply submitting 1 number.

mysql_query("INSERT test_mysql SET number='$number', uid='$uid'") or die(mysql_error()); 

but i have serached high and low and tried many examples and tried myself but im getting nowhere.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 3
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 28 '15 at 15:39
  • Thank you Jay but im am learning and finding alot of old examples/code and im slowly making what i need but getting stuck alot lol thanks – Mark Bennett Apr 28 '15 at 16:16

1 Answers1

0

There will be proper libraries to do this but given a simple csv file

a,b,c
1,2,3
4,5,6

then you can loop through it as demonstrated in the docs http://php.net/manual/en/function.fgets.php and get each line.

Then you can grab the value you want something like this.

$handle = @fopen("test.csv", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        /* this gives you the value */
        echo explode(',',$buffer)[0];
    }
   fclose($handle);
}

Which gives you the data you want

a
1
4

You would need to improve this to deal with real messy data and skip headers etc before you then inserted each number.

Another approach would be loading the csv directly into the database table https://dev.mysql.com/doc/refman/5.1/en/load-data.html