PHP & MySQL Tutorial
- Creating Forum using mysqli
bogotobogo.com site search:
Creating MySql Database
In this tutorial, I'll start from the web hosting case. In other words, I'll make a database from the Control Panel provided by the hosting site.
Select the MySQL Databases:
Create a new database:
We also need to create a user at least for the root.
Connecting to the database
I wrote a simple code that makes a connection to mysql database and creating a table called "Users".
The code (/Forums/php_includes/db_connection.php) looks like this:
<?php $con = mysqli_connect('localhost', 'db_user', 'password', 'db_name'); if (mysqli_connect_errno()) { die('Not connected : ' . mysql_error()); } else { echo "Successful connection!\n"; } /* creating "Users" table */ $sql = "CREATE TABLE Users ( id INT(11) NOT NULL auto_increment, username VARCHAR(16) NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, gender ENUM('m','f') NOT NULL, website VARCHAR(255) NULL, country VARCHAR(255) NULL, userlevel ENUM('a','b','c','d') NOT NULL DEFAULT 'a', avatar VARCHAR(255) NULL, ip VARCHAR(255) NOT NULL, signup DATETIME NOT NULL, lastlogin DATETIME NOT NULL, notescheck DATETIME NOT NULL, activated ENUM('0','1') NOT NULL DEFAULT '0', PRIMARY KEY (id), UNIQUE KEY username (username, email) )"; /* Check if table creation was successful */ if (mysqli_query($con,$sql)) { echo "Table Users created successfully!\n"; } else { echo "Error creating table: " . mysqli_error($con); } ?>
If we run it, we get:
# php db_connect.php Content-type: text/html Successful connection! Table Users created successfully!
We can check the created table using PhpAdmin:
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization