I have created a PDO connection successfully, and now I am trying to create an MYSQL table dump in .sql format.
I am able to generate an array with all the entries in an associative Click Here format
The first step that I did is to download a database dump manually, and I am posting it in the next post so that this post remains readable.
There were a lot of parts in that database, first one is →
CREATE TABLE `cars` (
`car_id` int(10) UNSIGNED NOT NULL,
`make_id` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
`yearmade` year(4) NOT NULL,
`mileage` mediumint(8) UNSIGNED NOT NULL,
`transmission` enum('automatic','manual') NOT NULL DEFAULT 'automatic',
`price` decimal(8,2) NOT NULL,
`description` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I have to generate this part programmatically
Next will be pulling data in a format which when restored should be accountable for inserting the value, including the creation of the table.
INSERT INTO `cars` (`car_id`, `make_id`, `yearmade`, `mileage`, `transmission`, `price`, `description`) VALUES
(1, 5, 2007, 113688, 'automatic', 13550.00, 'Green Chrysler 300. Only one owner, very carefully maintained. Top of the line, and beautifully styled, this is an outstanding ride with great performance.'),
(2, 2, 2007, 126570, 'automatic', 7545.00, 'Red Ford Focus. \"Great bargain as a family car.\"'),
(3, 12, 2012, 517, 'automatic', 20755.00, 'Demo model that\'s hardly been out on the road, this red Chevrolet Cruze is just a dream, with just about every option you could ask for. Great fuel economy, too.'),
(4, 16, 2010, 116626, 'automatic', 10554.00, 'Red Camry in good running condition. Sound electrics and bodywork. Clean interior, appears never to have been smoked in.'),
(5, 7, 2011, 24694, 'automatic', 26951.00, 'Space grey BMW 3 series with beige leather interior. BMW Factory Certified with a 6 year/100,000 mile warranty from in-service date.'),
(6, 11, 2005, 95496, 'automatic', 8554.00, 'Black Jaguar S-Type in perfect working condition. Good electrics and bodywork. Low mileage. New tires recently fitted.'),
(7, 5, 2004, 75500, 'automatic', 6005.00, 'Black Sebring LX convertible. Very low mileage. Excellent ride. A must-see bargain.'),
(8, 4, 2001, 100145, 'automatic', 9550.00, 'Black SLK-Class convertible. Immaculate interior. Power top and power seats. Runs like new.'),
(9, 2, 1999, 102500, 'automatic', 4550.00, 'Metallic red Mustang convertible. Economy car, very easy on fuel. No negative history. No rust or damage on paintwork.'),
(10, 16, 2002, 173658, 'automatic', 6550.00, 'So much to like about this Silver Toyota 4Runner. Runs well, good paint, tires, nice sound system.'),
(11, 3, 2005, 122250, 'automatic', 11550.00, 'Black Cadillac SRX. Only one owner. Beautiful SUV.'),
(12, 17, 2002, 155500, 'automatic', 4305.00, 'Silver Passat. Only one owner. Leather interior. Rare bargain.'),
(13, 1, 1952, 46383, 'manual', 22055.00, 'Burgundy Studebaker Roadster with newly rebuilt engine and wide whitewall tires. Three-speed manual transmission. Runs and drives amazingly.'),
(14, 10, 2006, 124209, 'automatic', 9120.00, 'White Santa Fe. Only one owner. Leather interior and bodywork are in great shape.'),
(15, 10, 2012, 9811, 'automatic', 24554.00, 'Silver Genesis with beige leather and wood trim interior. Great handling and comfort. Low, low mileage. Luxury at an affordable price.'),
(16, 14, 2005, 130500, 'manual', 7505.00, 'Five-speed manual black Civic. Super clean, with 6 CD changer. This one, you must see!'),
(17, 15, 2007, 84947, 'automatic', 14554.00, 'Audi A4 Quattro. Gray with gray leather interior, and glass roof. Excellent value.'),
(18, 6, 1972, 77600, 'manual', 28055.00, 'Citroen D Super with 5-speed manual transmission in fantastic shape. Extremely well maintained, and has obviously been treasured by its owner. A real European classic.'),
(19, 8, 2012, 19361, 'automatic', 14553.00, 'Yellow Fiat 500 POP. Immaculate interior and bodywork. Electrics in perfect order. Tires only slightly worn.'),
(20, 12, 2002, 160550, 'automatic', 4545.00, 'Blue Impala LS with gray interior. Ideal economical vehicle with good gas mileage. Dependable engine, new tires. Price includes 6 month/8,500 mile warranty.'),
(21, 9, 2005, 94995, 'automatic', 8150.00, 'Gold Pontiac Bonneville with low mileage. Great condition.'),
(22, 5, 2006, 102300, 'automatic', 7105.00, 'Green Town & Country sports van.'),
(23, 8, 2012, 5238, 'automatic', 16050.00, 'Pearl white Fiat 500 sport hatchback. Power glass sunroof and power windows. Only one owner.'),
(24, 17, 2005, 70388, 'automatic', 14055.00, 'Shadow blue Touareg in excellent condition. Heated leather seats, sun roof, and navigation. Really low mileage. '),
(25, 13, 2012, 35000, 'automatic', 15550.00, 'Tan Altima. Low mileage. Excellent condition.'),
(26, 2, 2004, 108694, 'automatic', 10980.00, 'Top of the line black Expedition XLT 5.4 liter 4WD with every conceivable option. A truly exceptional SUV.'),
(27, 2, 2005, 123059, 'automatic', 8000.00, 'Blue Ford Escape. Excellent condition. A real bargain.'),
(28, 13, 2010, 32791, 'automatic', 20505.00, 'Red Pathfinder 4WD. Only one owner. Nicely equipped with just about every feature you could want, including third-row seats.'),
(29, 14, 2002, 124334, 'automatic', 6004.00, 'Silver Accord with sunroof, CD player, and all new tires. Excellent condition.'),
(30, 14, 2011, 27345, 'automatic', 14000.00, 'Dark gray Civic. Only one owner, very low mileage. Great fuel economy.'),
(31, 12, 2011, 34256, 'automatic', 12000.00, 'Dark gray Malibu. Interior and bodywork in good condition. Low mileage.'),
(32, 15, 2003, 93494, 'automatic', 10000.00, 'Silver Audi A6 with tan interior. Two previous owners. Mechanically sound and good bodywork. New tires.'),
(33, 3, 2005, 139534, 'automatic', 11505.00, 'Pearl white Cadillac SRX. Electrics, engine, and bodywork all in excellent condition. Only one owner. Tires have about 3/4 of their life span left.'),
(34, 7, 2011, 33784, 'automatic', 25904.00, 'White 3 Series 328i. Low mileage. Bodywork in mint condition. AM/FM stereo, a trip computer, power sunroof.'),
(35, 5, 2012, 7834, 'automatic', 16560.00, 'White Chrysler 200 with black interior. Exceptionally low mileage ');
I have done a course so I have some Idea how to backup in terms of headers
, but .sql
seems to be very tough. Headers will be used. Like this →
header('Content-Type: text/plain');
header('Content-Disposition: attachment;filename=cars.csv');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
$csvoutput = fopen('php://output', 'w');
$row = getRow($result);
$headers = array_keys($row);
fputcsv($csvoutput, $headers);
fputcsv($csvoutput, $row);
while ($row = getRow($result)) {
fputcsv($csvoutput, $row);
}
fclose($csvoutput);
exit;
Ley say if i want to get this part first in .sql
file are there any functions in PDO or we have to rely on core PHP?
CREATE TABLE `cars` (
`car_id` int(10) UNSIGNED NOT NULL,
`make_id` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
`yearmade` year(4) NOT NULL,
`mileage` mediumint(8) UNSIGNED NOT NULL,
`transmission` enum('automatic','manual') NOT NULL DEFAULT 'automatic',
`price` decimal(8,2) NOT NULL,
`description` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
There doesnt seems to be any function similar to this →
fputcsv($csvoutput, $row);
for .sql