SHARE
Facebook X Pinterest WhatsApp

PHP Tutorial – Databases in PHP – MySQL in PHP – hooking up

Written By
thumbnail Vince Barnes
Vince Barnes
Jun 23, 2005

Since we are about to get into the use of a specific DBMS in PHP, namely
MySQL, I should warn you of one thing to watch out for regarding this DBMS. 
In the past, the support for MySQL, including all the functions used to interact
with your MySQL database, was built in to PHP.  When they came out with PHP
5, however, this support was no longer included.  Obviously, if the support
isn’t there, it won’t work — in fact you’ll get error messages saying
"undefined function mysql_……"   If you get such a message it means
that the support for MySQL will have to be added to your PHP.  How to do
that is beyond the scope of this tutorial, but you’ll  find help at the
MySQL website
http://www.mysql.com
and/or at the PHP website
http://www.php.net where you’ll
find the PHP Manual, which has all kinds of good things you’ll want to know.

Connect to MySQL Server

The first thing you’ll want to do is to establish a connection to your MySQL
database server.  This is accomplished with the mysql_connect function.
Here’s an example:

$connid = mysql_connect (‘servername’ , ‘username’ , ‘password’);

$connid is an identifier (a positive integer, returned by the mysql_connect
function) that identifies this connection and is used in subsequent function
calls to tie those calls to this connection.  The mysql_connect function is
provided three parameters, the name of the server (commonly ‘localhost’ when the
server is on the same computer as PHP), the username and password authorized to
access the server.

Next, we need to check that the connection was successfully established. 
For the sake of this example we are going to print a message on the screen to
let us know of whether or not the call was good.  In practice, you will
probably take a different course of action (see also the "error control
operator, below.)

if ($connid = = false)
 { print "Server connection failed";  }
else
 { print "Server connected!"; }

When we are all done, it is a sound programming practice to close things that
we have opened.  To close our connection we will use the mysql_close
function.  Notice that we use the $connid to reference the connection that
we opened.

mysql_close ($connid);

Nice and tidy!
 

Using the Error Control Operator

The error control operator can be used to prevent PHP from displaying an
error message in the user’s browser when the attemp to connect to the MySQL
server fails.  In the real world, this is the more common requirement. 
The error control operator is an @ sign placed next to the function call like
this:

$connid = @mysql_connect (‘servername’ , ‘username’ , ‘password’);

Using the die function

Another common real world practice is to use the die function to terminate
the program and display a message.  Without the connection to the database,
it’s most likely that there’s no point in continuing!

$connid = @mysql_connect (‘servername’ , ‘username’ , ‘password’) or
die("Connection to the DBMS server failed");

 

Now that we have
connected to the server, we’ll need to select a database and start working with
it.  That’s next!  (Coming soon)

Recommended for you...

PHP vs Javascript: Compare Web Development Languages
How Laravel Speeds Up App Development
Rob Gravelle
Aug 29, 2018
7 Ways to Start Learning JavaScript as a WordPress Developer
Exploring PhpSpreadsheet’s Formatting Capabilities
Rob Gravelle
Feb 26, 2018
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.