Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Database Access With Php

Rated 3.56 (Ratings: 3)

Want more?

  • More articles in Code
 

Burhan Khalid

Member info

User since: 08 Aug 2001

Articles written: 3

One of the best things about PHP is its built in functions for database access. In this post, I will provide some examples on how to access a MySQL database using PHP. My free PHP host allows MySQL databases, so this came in very handy while trying my hand at PHP (I was a seasoned ASP programmer...ah...those good old days). I won't comment general code syntax, but will highlight database specific portions. For PHP help, try one of the many PHP sites on the 'net. The big mamma is of course http://www.php.net. Before I let you in on the code, let me explain what this code does. It was designed for a gateway for a link library. The table in my database is called links, and I query it for a short description and links to the site. This way, I can create a link repository and categorize the sites. The table has the following fields : siteID, siteDescription, siteTitle, siteLink, siteLogo, all VARCHAR32 types. Now, without further delay on to the code!

/* --- [ PHP Database Connection Code ]---

--- [ Copyright (C) Mohammad Burhan Khalid ]---

--- [ Data Dictionary ] ---

$_host = path your your MySQL server

$_user = your user name (duh)

$_pwd = your password (duh)

$_db = the database name you want to connect to

$handle = returns a "handle" to your database.

we will use this to check the status of the

connection

$strDestination = the "siteID" for the site that we want to lookup. I passed it as a parameter to the gateway page.

Something like test.php?destination=ms

*/

$_host="database.mysite.com";

$_user="admin";

$_pwd="";

$_db="siteDB";

$handle = mysql_connect($_host,$_user,$_pwd);

if ($handle) { $errorCode=""; }

else { $errorCode="Can't Open Database ".$_db." ]";}

mysql_select_db($_db);

$strDestination=$HTTP_GET_VARS["destination"];

$strQuery = "SELECT * FROM links where siteID LIKE '".$strDestination."'";

$intQID = mysql_db_query($_db, $strQuery);

$QueryResult = mysql_fetch_assoc($intQID);

$siteTitle = $QueryResult["siteTitle"];

$siteDescription = $QueryResult["siteDescription"];

$siteLogo = $QueryResult["siteLogo"];

$siteLink = $QueryResult["siteLink"];

mysql_close($handle);

Now on to the explanation!

mysql_select_db($_db);

This line selects the database that you want to work with in MySQL. Takes a parameter of type string (but since all parameters in PHP are variant till runtime, all you need is a set of quotes around your variable. Ours was $_db).

$intQID = mysql_db_query($_db, $strQuery);

This little snippet does all the magic. It excutes the query and returns a handle (type int) to your query.

$QueryResult = mysql_fetch_assoc($intQID);

I used this function to return the results of the query as an associative array. Used the "handle" returned by the mysql_fetch_assoc() function.

$siteTitle = $QueryResult["siteTitle"];

This is a way to get the results from the table name. You can use numeric identifiers also, ie $QueryResult[0];. Nothing too fancy here, just getting elements from an array.

mysql_close($handle);

After we are done, its always nice to close our connection. We use the same "handle" that was returned when we opened the connection.

This is a very simplified way to connect to a MySQL database. Leave me your comments, and I might write a more advanced (say, looping through the fields, search for records, paginate, etc.) tutorials.

Leave me feedback, or email me with your comments.

I'm a college student finishing up my Bachelor of Science degree at Southwestern Oklahoma State University. I generally try to help out at evolt, answering questions on the list, and occasionally writing an article or two. I am the current president of the computer club, the only computer science tutor, and the webmaster for our site. In addition to traveling around the world (I speak 5 languages), in my free time I like to fly (both sims and real aircraft), read, and play around on the internet.

That was 8 years ago - now I'm a IT consultant, e-commerce and Linux SME in Kuwait. I currently work for a large engineering consultancy firm where I am responsible for IT strategy for the company and consulting with client on their large-scale IT projects.

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.