Guest User

Untitled

a guest
Jan 10th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?php
  2. $servername = "localhost:3307";
  3. $username = "root";
  4. $password = "root";
  5. $dbname = "patata";
  6.  
  7. // Create connection
  8. $conn = new mysqli($servername, $username, $password,$dbname);
  9.  
  10. // Check connection
  11. if ($conn->connect_error) {
  12. die("Connection failed: " . $conn->connect_error);
  13. }
  14. echo "Connected successfully <br>";
  15.  
  16. // Create database
  17. $sql = "CREATE DATABASE patata";
  18. if ($conn->query($sql) === TRUE) {
  19. echo "Database created successfully<br>";
  20. echo "<br>";
  21. } else {
  22. echo "Error creating database: " . $conn->error;
  23. echo "<br>";
  24. }
  25.  
  26. // sql to create table
  27. $sql = "CREATE TABLE MyGuests (
  28. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  29. firstname VARCHAR(30) NOT NULL,
  30. lastname VARCHAR(30) NOT NULL,
  31. email VARCHAR(50),
  32. reg_date TIMESTAMP
  33. )";
  34.  
  35.  
  36. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  37. VALUES ('John', 'Doe', 'john@example.com')";
  38.  
  39. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  40. VALUES ('John', 'Doe', 'john@example.com');";
  41. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  42. VALUES ('Mary', 'Moe', 'mary@example.com');";
  43. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  44. VALUES ('Julie', 'Dooley', 'julie@example.com')";
  45.  
  46.  
  47. if ($conn->query($sql) === TRUE) {
  48. $last_id = $conn->insert_id;
  49. echo "Table MyGuests created successfully. Last inserted ID is: " . $last_id;
  50. } else {
  51. echo "Error creating table: " . $conn->error;
  52. echo "<br>";
  53. }
  54.  
  55. $conn->close();
  56. ?>
Add Comment
Please, Sign In to add comment