· 6 years ago · May 24, 2019, 03:22 AM
1<?php
2
3 error_reporting(E_ALL);
4 ini_set('display_errors', 1);
5
6 $server = "localhost";
7 $user = "root";
8 $pass = "root";
9 $db = "prepared_php";
10
11 $connection = new mysqli($server, $user, $pass, $db);
12
13 $create = "CREATE DATABASE IF NOT EXISTS prepared_php";
14
15 $table = "CREATE TABLE IF NOT EXISTS restaurants (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, city_name VARCHAR(255), cuisine VARCHAR(255), rating VARCHAR(32))";
16
17 $insert = 'LOAD DATA LOCAL INFILE "path/to/csv/here/example.csv"
18 INTO TABLE restaurants
19 FIELDS TERMINATED BY ","
20 ENCLOSED BY \'"\'
21 LINES TERMINATED BY "\r\n"
22 IGNORE 1 LINES
23 (city_name, cuisine, rating)
24 SET ID = NULL';
25
26 $select = "SELECT * FROM restaurants";
27
28 if($connection->connect_error){
29 die("This no work essay: " . $connection->connect_error . "\n");
30 }
31
32 $db_create = $connection->query($create);
33
34 if($db_create === TRUE){
35 echo "New database created \n";
36 }
37 else {
38 echo "Something wrong essay: " . $connection->error . "\n";
39 }
40
41 $db_table = $connection->query($table);
42
43 if($db_table === TRUE){
44 echo "New table created \n";
45 }
46 else {
47 echo "Something wrong, essay: " . $connection->error . "\n";
48 }
49
50 $db_insert = $connection->query($insert);
51
52 if($db_insert === TRUE){
53 echo "Data imported correctly";
54 }
55 else {
56 echo "Something wrong, essay: " . $connection->error . "\n";
57 }
58
59 $rows = $connection->query($select);
60
61 if($rows->num_rows > 0){
62 while($row = $rows->fetch_assoc()){
63 print_r("\nCity: " . $row["city_name"] . "\nDiet: " . $row["cuisine"] . "\nRating: " . $row["rating"]);
64 }
65 }
66 else {
67 echo "There are no results to display \n";
68 }
69
70 $connection->close();
71
72?>