I've been trying for many days to export from a query some values in .csv
with MySQLi, but I have a problem when the export is made, everything is exporting to a single column, instead I need that the export should make like SQL code (I have 3 selected columns and I need 3 exported columns in .cvs
).
Bellow you can find two codes:
1) This code creates a data base, table and insert the values:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
// Create connection pt a crea tabela
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Create connection pt a insera valori
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert value in db
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe, Mark', 'john@example.com'),
('Mary', 'Moe-Johny', 'mary@example.com'),
('Julie', 'Dooley', 'julie@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
2) This code is for the export in .csv
:
<?php
//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=export.csv');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I'm using this XAMP version:
Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.1
Database client version: libmysql - mysqlnd 5.0.12-dev - 20150407 - $Id: 7e72f9690b1498a1bead7a637c33a831c0d2f655 $
PHP extension: mysqli Documentation
PHP version: 7.0.1
This version creates some problems for the MySQL code, that's why I prefer the code in MySQLi. Thank you very much!
`. So at the very least you'd have to tell the importing apllication to use `-` as the delimiter; but I doubt that the result is what you want. You might be interested in http://docs.php.net/fputcsv – VolkerK Feb 05 '16 at 09:11