How to Create a PHP Database on JustHost
JustHost is a Web hosting provider that sells packages with cPanel, a popular hosting management application. This application includes a number of useful tools, such as phpMyAdmin. Using phpMyAdmin, you can create databases and users. Once you create a database, make a PHP file and practice connecting to MySQL using PHP code. When you create a table in PHP, you can check its contents in phpMyAdmin as well.
Instructions
-
-
1
Log in to your Web hosting account from the JustHost website. Open phpMyAdmin from the cPanel and type a name for your new database in the "Create new database" field.
-
2
Click the "Privileges" tab, and select "Add a new User" if you need to create a new user for the database. Complete the form for the new user and generate a password. Copy down the password in a safe place, because you will need it to program with your database in PHP. Select "Grant all privileges on database 'namehere'" and click "Go" at the bottom of the page.
-
-
3
Create a new file in Notepad or a code editor and paste in this code:
$user = 'username_here';
$password = 'password_here';
$db_conn = mysql_connect(localhost, $user, $password) or die ('Unable to connect to MySQL');
This PHP code connects to MySQL. Change "username_here" and "password_here" to the username and password for the MySQL user you created in phpMyAdmin.
-
4
Add this code below the MySQL connection code:
$db_select = mysql_select_db('database_name', $db_conn);
Use this code to select the database you want the code to work with. Change "database_name" to the name of your database.
-
5
Insert a table via PHP using the following code:
$sql = "CREATE TABLE customers (
first_name varchar(11),
last_name varchar(11),
id int auto_increment,
PRIMARY KEY id)";
You can reuse the "$sql" variable as many times as you need to insert tables or perform others MySQL queries. The variable must hold MySQL code as shown in the example. Set a primary key -- an index for the table -- using an auto-incrementing ID field.
-
6
Run the MySQL code in your "$sql" variable using the "mysql_query()" function:
mysql_query($sql, $db_conn);
-
7
At the end of your file, close the MySQL connection:
mysql_close($db_conn);
-
8
Save your file with the ".php" extension and upload it to your JustHost account using either an FTP client or the file management tool in your cPanel. Navigate to the PHP file in your browser and check that the page does not display errors. If the code works, then you will see no errors printed. You can see your new table if you view the database in phpMyAdmin.
-
1
Tips & Warnings
Creating a user with privileges for just one database strengthens your website's security.