Skip to main content

PHP Database Connection using NetBeans IDE 8.1 with WampServer

PHP Database Connection using Netbeans IDE with WampServer


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

1. Netbeans IDE: I'll be using version 8.1.

2. WampServer: Download from here.
3. A browser: You can use any browser of your choice

If you do not know how to link PHP in NetBeans with WampServer, then follow this link.

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.

Now to create the database, on your browser open localhost/phpmyadmin.


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)
);


Now to create a table, click on the SQL tab





                                                                                 



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.

To view to just inserted record

      
And there you have it, the just added record.

the just added record.


That's it!
Happy coding. 


Comments

Popular posts from this blog

Can I use Mendeley with LibreOffice Writer (on Windows)?

The answer is a big yes! Mendeley works with Microsoft Word as well as LibreOffice Writer. As you may know, LibreOffice is a free and open source software suite and can be downloaded from here . To get Mendeley, download from here . Having installed the two software, do the following: 1: Open Mendeley, go to Tools → Install LibreOffice Plugin 2 : Open LibreOffice Writer. You should see the newly added Mendeley tools just below the menus (as seen below). And that does it!

Working with PHP in NetBeans 8.1 using WampServer

The aim of this tutorial is simple to show you how to work with PHP using WampServer with the NetBeans IDE 8.1