'I want to export data from 2 different tables in mysql and convert to csv maybe using php
I am not a very good coder, but I have a Zencart store.. and all I need is to get my products in Pinterest I got as far as getting one table in export to CSV file.. I can download it to my computer or save it to the server.. but I can not for the life of me figure out how to get the second table to connect the data. (thank you in Advance) this is the table I want
the table names are: products and products_description
the rows that need to be extracted are:
from products
- -products_id
- -products_image
- -products_price
from the table products_description
- -products_name
- -products_description
here is what I got so far. how to add the next table.
<?PHP
// (A) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
$dbHost = "localhost";
$dbName = "database name";
$dbChar = "utf8";
$dbUser = "username";
$dbPass = "cxcxcxccxcc";
try {
$pdo = new PDO(
"mysql:host=$dbHost;dbname=$dbName;charset=$dbChar",
$dbUser, $dbPass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NAMED
]
);
} catch (Exception $ex) { exit($ex->getMessage()); }
// (B) HTTP CSV HEADERS
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"export.csv\"");
// (C) GET USERS FROM DATABASE + DIRECT OUTPUT
$stmt = $pdo->prepare("SELECT * FROM `products`");
$stmt->execute();
while ($row = $stmt->fetch()) {
echo implode(",", [$row["products_id"], $row["products_image"],
$row["products_price"]]);
echo "\r\n";
}
Solution 1:[1]
You can use the table Join to connect 2 tables. This link a reference to get started
Update:
you must have a foreign key to the products_description table,
let's say you added product_id column in product_description table, so we will connect the table with the product id.
SELECT `products`.products_id, `products`.products_image, `products`.products_price, `products_description`.products_name, `products_description`.products_description
FROM `products`
LEFT JOIN `products_description`
ON `products`.`id` = `products_description`.product_id
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
