read csv file in php

$file = fopen( 'insertCSVPathFileHere', 'r' );

while ( $line = fgetcsv( $file, 0, ',' ) ) print_r( $line );

fclose( $file );
Advertisements

sqlite3 with php

1 – Install sqlite3 and sqlite module for php

apt-get install sqlite3 php5-sqlite

2 – Create a database file with sqlite command or use sqlitestudio

sqlite3 test.db
sqlite> CREATE TABLE tbl1(name VARCHAR(9), age INT(2));
sqlite> INSERT INTO tbl1 VALUES('nickollas',33);
sqlite> INSERT INTO tbl1 VALUES('carvalho', 20);

3 – Create a php file to access data in database

$db = new SQLite3('test.db');

$rs = $db->query( 'SELECT rowid,name,age FROM tbl1' );

while ( $row = $rs->fetchArray() ) echo $row['rowid'], $row['name'], $row['age'], "\n";

– Some sqlite commands:

sqlite> .tables show all tables
sqlite> .schema show table structure
sqlite> .quit exit of sqlite

encode and decode array to json php

// raw array
echo "\t", 'raw array', "\n\n";
$arr = ['a', 'b', 'c' => 'd'];
echo '$arr output: '; print_r( $arr );

// encode array
echo "\t", 'encode array', "\n";
$arr_enc = json_encode( $arr );
echo "\n", 'encode array output: '; print_r( $arr_enc ); echo "\n\n";

// decode array
echo "\t", 'decode array', "\n\n";
echo 'array decode output: '; print_r( json_decode( $arr_enc ) ); echo "\n";
echo 'get some values: ', json_decode( $arr_enc )->{0}, ' ', json_decode( $arr_enc )->{'c'}, "\n\n";
echo 'get all values: ';
foreach( json_decode( $arr_enc ) as $value ) echo $value, ' ';