In exporting CSV file using PHP, there are some records that won't be included. It seemed like while transferring, it stopped even if there are still records to be exported. What's the problem?
A:
i have experience several error while exported CSV content to database. most of the problem is because database fail while save record. for example you have data like this :
"1;me;24"
"2;you;"
"3;they;26"
"4;she;27"
"5;it;28"
if your database have the constrain the row cannot be null, so the database will throw error to php. and the php will stop loop before the all of record recorded to database.
the solution, you can add exception at your code, when the database fail to insert to database, it will continue the loop until it finish recorded csv to your database.
<?php
error_reporting(E_ERROR);
class MySQLException extends Exception
{
public function __construct($message,$code=0){
parent::__construct($message,$code);
}
}
class Mysql
{
private $_DB_HOST;
private $_DB_USERNAME;
private $_DB_PASSWORD;
private $_DB_NAME;
private $_DB_SELECT_STATE;
private $_CONNECTION;
public function __construct($host, $username, $password, $dbname) {
$this->_DB_HOST = $host;
$this->_DB_USERNAME = $username;
$this->_DB_PASSWORD = $password;
$this->_DB_NAME = $dbname;
$this->__connect();
$this->__select_db();
}
private function __connect() {
$this->_CONNECTION = mysql_connect($this->_DB_HOST, $this->_DB_USERNAME, $this->_DB_PASSWORD);
if(!$this->_CONNECTION) {
throw new MySQLException('Could Not Connect to database with given setting');
}
}
private function __select_db() {
$this->_DB_SELECT_STATE = mysql_select_db( $this->_DB_NAME , $this->_CONNECTION);
if(!$this->_DB_SELECT_STATE) {
throw new MySQLException('Could Not select to database with given setting');
}
}
public function query($query){
if( ( $result = mysql_query($query, $this->_CONNECTION ) )) {
return $result;
} else {
$command = explode(' ', trim($query));
throw new MySQLException('Could not do' . $command . ' query');
}
}
}
// connect to database
$database = new Mysql("localhost", "username", "password", "test");
// example your csv file before parsed
$csv = array("1;me;24", "2;you;", "3;they;26", "4;she;27", "5;it;28");
for($i = 0; $i < sizeof($csv); $i++){
try{
$row = explode(";", $csv[$i]);
$database->query("INSERT INTO testtable (id, name, age) VALUES (".$row[0].", '".$row[1]."', ".$row[2].")");
} catch (MySQLException $e){
echo 'We could not insert to the database <br>';
}
}
if you wasn't add any try catch, your code will stop while inserting the second query
"1;me;24"
"2;you;"
"3;they;26"
"4;she;27"
"5;it;28"
but the code remain loop till end parsing if you catch the exception.
Swing Magic
2010-08-18 08:39:56
"you can add exception at your code, when the database fail to insert to database" - What do you mean? Add something in my insert sql statement? Can you explain further? Thank you!
anonymous123
2010-08-20 07:50:26
ok, i will add the example implementation
Swing Magic
2010-08-20 17:56:08
Thank you for your reply and example, Swing Magic.I don't think that the problem has to do with the inserting of records into the database since when I export the CSV file, I don't do insert query, I just use select query to retrieve the desired records stored in the database. By the way, in exporting CSV file, there are datapickers which allow users to select the date range. The problem will occur here however, if I select only one date, it works fine. The problem only occurs when I select the date range.
anonymous123
2010-08-23 02:27:11
I tried mysql_num_rows to check whether it successfully fetched all the records from the database after select query, and the result is right but when I open the CSV file not all records will appear.
anonymous123
2010-08-23 02:27:34