· 9 years ago · Jun 26, 2017, 09:14 AM
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 }
36else
37 {
38 echo "Error creating database: " . mysqli_error($con);
39 }
40
41//Table structure for table `Users`
42if(!$con )
43{
44 die('Could not connect: ' . mysql_error());
45}
46echo '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;
55mysqli_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.
58if(!$retval)
59{
60 die('Could not create table: ' . mysql_error());
61}
62else
63{
64 echo "Table created successfully\n";
65}
66mysqli_close($con);
67
68 ?>