The aim of this tutorial is to show you how to use PHP in connecting to a MySql Database using the NetBeans IDE 8.1. Have it in mind that there are several ways of doing this but we'll be looking at just one - a basic one.
Basic software requirements
from here.
3. A browser: You can use any browser of your choice
1. Netbeans IDE: I'll be using version 8.1.
2. WampServer: Download
3. A browser: You can use any browser of your choice
Step 1: Create a project, name it "Database_Connection".
Now time to create our database.
Step 2: Now to create the database, on your browser open www.localhost/phpmyadmin.
Type in the name of the database as Company as shown in the image below then click Create.

name VARCHAR(30),
department CHAR(30),
PRIMARY KEY (sId)
);

Now, we are moving back to the Netbeans IDE.
Step 4: Open the index.php file under the "Source Files" folder. Delete everything inside this file and type or paste the code below.
And there you have it, the just added record.

That's it!
Happy coding.
Now time to create our database.
Step 2: Now to create the database, on your browser open www.localhost/phpmyadmin.
Type in the name of the database as Company as shown in the image below then click Create.

Now to create a
table, click on the SQL tab and type in this code in the textbox as
seen below.
CREATE TABLE staff(
sId INT NOT NULL,name VARCHAR(30),
department CHAR(30),
PRIMARY KEY (sId)
);

Click the Go button
to execute the query.
Now we're done with
creating the table, you can see it below.
Now, we are moving back to the Netbeans IDE.
Step 4: Open the index.php file under the "Source Files" folder. Delete everything inside this file and type or paste the code below.
$dbHost = "localhost";
$user = "root";
$password = "";
$dbName = "Company";
$mysqli = new
mysqli($dbHost, $user, $password, $dbName);
// This will return
true if an error was encountered during the database //connection
if
($mysqli->connect_errno) {
echo "Error in
connectring to Server!";
exit();
}
// Put the SQL query in a string
$query = "INSERT
INTO staff (sId, name, department) VALUES (1, 'Ben', 'Sales') ";
//Tests if query executes or not
if
($mysqli->query($query)) {
echo
"Successful. Record added!";
} else {
die("Error in inserting record to database!");
}
Note: The password field is empty because there is no password securing the DBMS - MySql. This is actually the default setting. If yours has a password use it in place of the empty string.
Run the file.
On your browser you
should get this message “Successful.
Record added!”.
To
view to just inserted record, open www.localhost/phpmyadmin, click on
Company at the left-side of the page and then click on the browse icon.
And there you have it, the just added record.

That's it!
Happy coding.
Comments
Post a Comment