Advertisement
Guest User

CreateUserTable.php

a guest
Jun 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2. // createTableUsers.php
  3. // version 1.0
  4. // Generation Time: 19/3/14
  5. // Server version: 5.6.11 MySQL Community Server (GPL)
  6. // PHP Version: 5.5.3
  7. // Database: `user_logon`
  8. // --------------------------------------------------
  9. // This file creates a database called user_logon.
  10. // This file also establishes a connection to MySQL,
  11. // and creates the database and then, a table called Users.
  12. // If the database or the table could not be created,
  13. // an error message is created.
  14. // --------------------------------------------------
  15. $host="localhost"; // Host name
  16. $username="root"; // Mysql username - USING ROOT IS INSECURE
  17. $password=""; // Mysql password - BLANK PASSWORD IS INSECURE
  18. $db_name="UserData"; // Database name
  19. $tbl_name="Users"; // Table name
  20.  
  21. //Connect to MySQL server using localhost as root user.
  22. $con=mysqli_connect($host,$username,$password);
  23. // Check connection
  24. if (mysqli_connect_errno())
  25. {
  26. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  27. }
  28.  
  29. // Create database
  30. //CREATE DATABASE IF NOT EXISTS site_db;
  31. $sql="CREATE DATABASE UserData";
  32. if (mysqli_query($con,$sql))
  33. {
  34. echo "Database created successfully";
  35. }
  36. else
  37. {
  38. echo "Error creating database: " . mysqli_error($con);
  39. }
  40.  
  41. //Table structure for table `Users`
  42. if(!$con )
  43. {
  44. die('Could not connect: ' . mysql_error());
  45. }
  46. echo 'Connected successfully<br />';
  47. $sql = ("CREATE TABLE Users(
  48. id INT(4) NOT NULL auto_increment,
  49. username VARCHAR(65) NOT NULL,
  50. password VARCHAR(65) NOT NULL,
  51. PRIMARY KEY (id))");
  52.  
  53. //flush???
  54. //USE user_logon;
  55. mysqli_select_db($con, 'UserData')or die("cannot select database");
  56.  
  57. $retval = mysqli_query($con, $sql); //mysqli_query always returns true if there was no error.
  58. if(!$retval)
  59. {
  60. die('Could not create table: ' . mysql_error());
  61. }
  62. else
  63. {
  64. echo "Table created successfully\n";
  65. }
  66. mysqli_close($con);
  67.  
  68. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement