Connect to database with php

Databases house the majority of website data for dynamic sites such as WordPress, Drupal, Joomla, etc. Getting to that data is done via the php scripts. Below I will show you how to access a database in php.

The most common method datbase transactions in php is the mysqli set of commands. This is a newer version of the old mysql commands. The mysql commands are deprecated, which means they are slated for removal in a future release of php.

Below, we see the syntax to the connect command:
mysqli_connect(host,username,password,dbname);
You can see it consists of four separate parts. The first is the host. This is the server that you will be connecting to. Most often the host is set to ‘localhost’. This means the database server is located on the same server as the php script. The next three are self explanatory, they are the database username, password, and the database name.

Connecting to the database
To connect, you simply need to call the ‘mysqli_connect()’ function. The code actually making a connection to a database would look something like this:
<?php
$con=mysqli_connect(“example.com”,”fred”,”password”,”my_dbname”);
?>

Checking the connection
Never trust that a connection will always work. You should test to see if it was successful, and if not, give an error message. The entire snippet of code should look something like this:
<?php
// Create connection
$con=mysqli_connect(“example.com”,”peter”,”abc123″,”my_db”);

// Check connection
if (mysqli_connect_errno()) {
  echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}
?>

Closing the connection
Once the connection has been made and you have run your query, it is a good idea to close the connection. This code is very simple and is done by calling the ‘mysqli_close()’ function. It looks like this:
mysqli_close($con);

 

 

8 Reasons to backup your site.

download

Imagine yourself relaxing on a Friday night after a long week of working on your site. You have been working diligently tweaking, adding content, and performing SEO so your site can climb higher in the search engine rankings. You go to check the site once more before signing off for the long weekend you have planned, and your site has disappeared! Was it a hacker? Did the intern delete the files? Whatever it was, all the work you did this week was for nothing, especially if the host doesn’t have a copy! Or was it?

Backups are very important to the health of your site. If you have a habit of making backups of your website, this may simply just be a minor inconvenience. If you do not have that habit, you may be in for a very long weekend restoring your site. Which situation do you prefer? I thought so.

Think of backups as your insurance against unforeseen circumstances. Just like the insurance for your car, it helps protect you from the inevitable accident that is always lurking around the corner. Below are eight great reasons why you should always have a personal backup your website.

8 Reasons to make site backups

  1.  Insurance. As mentioned before, the backup serves as insurance against any catastrophes that may occur to your data.
  2. Don’t rely on your host. This is not to say hosting backups are bad, but if you notice your mistake too late, the host backup copy may have run and the corrupt or missing files will be in their backup as well.
  3. Accidents happen. The intern you just hired may be slightly less savvy than you and not notice they overwrote your latest work. Or perhaps you deleted the wrong file by mistake when making the updates.
  4. Easy rollback. If you make a backup before going on an update spree, you can always restore the backup if you did not like the results. This is much easier than manually undoing all the changes you just made.
  5. It’s easy. Backups take just a few moments and can even be automated. Anything this easy that can save your goose should be a no-brainer.
  6. Testing. Having extra database backups can come in handy for creating duplicate test runs. This way you do not mess up your production data.
  7. Peace of mind. Sleep better knowing the high school hacker cannot permanently destroy your site while you dream.
  8. Corruption protection. Files and databases can become corrupt. It is better to lose a days worth of data than the last three years of your blog.

With all these reasons for backing up your site, it doesn’t make sense not to! For those with cPanel accounts, the manual process is very easy. You can make backups of your data at any time and download it to your local computer for safe keeping. See just how simple it is in the article link below.

Backing Up Your Website