ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

PHP & MySQL: Creating Connection to MySQL Database

Updated on June 29, 2011

MySQL database is widely used with PHP. Before using MySQL with PHP, you must have to create a connection to the database of MySQL. If you don't create the connection then you can't access to database form the php code. This page describes the process of creating connect to MySQL database. This is done by using mysql_connect() function. To learn totally, just keep reading...

How to create connect to MySQL

Actually, MySQL is a database. You can access MySQL database from php by using one function. The function is mysql_connect( ). If you don't use this function, you can't connect the database from php. mysql_connect( ) has three parameters. The full function looks like the following line:

mysql_connect (servername, username, password) ;

mysql_connect() opens a non-persistent MySQL connection. From the above line, you can find that mysql_connect() has the following three parameters:

  1. servername: This is an optional parameter. It specifies the server for creating connection. The default value of servername is localhost:3306.
  2. username: This one is an optional parameter too. It means the user name to log in to the database.
  3. password: This is an option parameter also. It's the password of the user to login the database. If there is no password then the value will be " ".

Codes for creating connection

To connect the database you have to write some code using mysql_connect() function. One example is provided below:

/* creating a connection using mysql_connect() function */

<?php

$myConnection = mysql_connect("localhost","user","abc123");

if (!$myConnection)
  {
    die('Not possible to connect...' . mysql_error());
  }

/* all other codes */

?>

Explanation of the above code

From the above code, you can see that we declare mysql_connection() function by a variable $myConnection. mysql_connection() has three parameters. (1) localhost means the servername, (2) user means the name of the user to login the database and (3) the corresponding user's password, in this case the password is abc123.

Later die() function is used. It's used to verify i.e. if you don't able to connect to the database then it will show the message Not possible to connect...

That's all to create a connect to database of MySQL form php code. In the next writing, I will explain the connection closing process and it's importance.

© Written by rancidTaste

If you are enjoyed this post, please consider to give a thumbs up and leave a comment. You may Subscribe to rancidTaste's RSS feed to get new pages which will be delivered to your feed reader. You can also read more hubs by rancidTaste.

Want to express your knowledge and ideas, click here to join Hubpages.

It's simple, just like 1-2-3

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      BindraBindu 

      7 years ago

      Hi,

      nice one article, it helps a lot !!!!! thanks for sharing with us.

      Here is also a nice one article on how to connect php webpage with mysql database step by step, you may chek out the following url for more details......

      http://www.mindstick.com/Articles/1ac687d9-f163-46...

      Thanks !!!!

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)