Advertisement
Guest User

conecta.php

a guest
May 22nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. <?php
  2.  
  3. // Connect to MySQL
  4.  
  5. // 1. Create a database conexao
  6. $conexao = mysqli_connect('localhost','root','');
  7.  
  8. if (!$conexao) {
  9. die("Database conexao failed");
  10. }
  11.  
  12. // 2. Select a database to use
  13.  
  14. $db_selected = mysqli_select_db($conexao, 'loja');
  15.  
  16. if (!$db_selected) {
  17. // If we couldn't, then it either doesn't exist, or we can't see it.
  18. $queryDB = 'CREATE DATABASE loja';
  19.  
  20. if (mysqli_query($queryDB, $conexao)) {
  21. echo "Database loja created successfully\n";
  22. } else {
  23. echo 'Error creating database: ' . mysql_error() . "\n";
  24. }
  25. }
  26.  
  27. $queryProdutos = "SELECT * FROM produtos";
  28. $testaTabelaProdutos = mysqli_query($conexao, $queryProdutos);
  29.  
  30. function criaTabelaProdutos(){
  31. $queryProdutos = "CREATE TABLE produtos (ID int AUTO_INCREMENT PRIMARY KEY,nome varchar(255) NOT NULL)";
  32. return $queryProdutos;
  33. }
  34.  
  35. if (empty($testaTabelaProdutos)) {
  36. criaTabelaProdutos();
  37. if ($testaTabelaProdutos) {
  38. echo "tabela produtos criada com sucesso.";
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement