tags:

views:

88

answers:

1
// First, prepare the statement, using placeholders
$query = "SELECT * FROM tableName";
$stmt = $this->connection->prepare($query);

// Execute the statement
$stmt->execute();
var_dump($stmt->fetch(PDO::FETCH_ASSOC));

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
{
echo "Hi";
// Export every row to a file
fputcsv($data, $row);
}

Is this correct way to do and if yes than why do I get false value for var_dump and than it does not go into while loop and does not write into csv file.

Any suggestions ?

A: 

One thing that is missing is that you first need to open the csv file:

$fp = fopen('file.csv', 'w');

then you can put data into it:

fputcsv($fp, $data);

As for the var_dump printing out false, you would have to provide more information about what objects you are actually using I think. (ie: what is $this->connection ?)

I don't know if this will help, but try to do it without the $this->connection:

$config['database'] = 'sakila';
$config['host'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';

$d = new PDO('mysql:dbname='.$config['database'].';host='.$config['host'], $config['username'], $config['password']);
$query = "SELECT * FROM actor";
$stmt = $d->prepare($query);

// Execute the statement
$stmt->execute();

var_dump($stmt->fetch(PDO::FETCH_ASSOC));

$data = fopen('file.csv', 'w');

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    echo "Hi";
    // Export every row to a file
    fputcsv($data, $row);
}
SeanJA
I am calling this function from another class's function and therein am opening the file and than am passing data stream back to this function and then doing the process further. I am not sure as to why this is happening.
Rachel
Basically `$this->connection` points to an underlying PDO connection which is present in another Dao class which am using in my this class.
Rachel
Maybe you are not connected to the database and it is failing silently?
SeanJA
Not it is connected as am using different function and for them it is connected.
Rachel
Well... you could always var_dump($this->connection->errorInfo()) to see what all is in there, it might give you some idea of why it isn't working (var_dump($this->connection) probably won't help too much as PDO hides it's contents from that)).
SeanJA
It gives me array with Null element in it signifying that there is no errors occured. Not sure as to why it is not working though.
Rachel
@SeanJA: It worked now, probably there is some issue with pdo connection, also currently using this am just able to get content of the database, is there anyway wherein I can get header information for the database inside the CSV file ?
Rachel
Do you mean the names of the columns of the table? http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
SeanJA
Yes. Column names.
Rachel
How can I get column names into csv file, currently am only getting data present in the database into the csv file.
Rachel
You have to do two selects for that, first do the show columns (or select column_name from information_schema.columns where table_name='my_table'; ) one, then do the second select. Since column names rarely change, you could also hard code them in your php code instead (and update them as needed).
SeanJA