Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner



Read “The Oracle DBA of Tomorrow”



internet.commerce
Be a Commerce Partner
Hurricane Shutters
Desktop Computers
Disney World Tickets
Promos and Premiums
Prepaid Phone Card
Compare Prices
Online Shopping
Phone Cards
Televisions
PDA Phones & Cases
Shop Online
Memory
Shop
Web Design




All Talk, Little Action on 'Net Neutrality Front?

Compliance Issues Still Bedevil IT

Enterprise Spending On Virtualization To Rise

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MySQL
September 24, 2002
Using a MySQL database with PHP
By Ian Gilfillan

It's time to add some dynamic content to your website. The best choice for ease-of-use, price and support is the combination of PHP and MySQL. This article introduces the beginner to using MySQL with PHP.

Download the files used in this tutorial here

A beginners guide to using MySQL with PHP

This article assumes a few things.

  • You know how to use PHP. If you don't, the best place to start is by reading Welcome to PHP
  • You know how to run basic queries in a database. If you don't, I suggest you visit Getting started with SQL or parts 1 and 2 of Introduction to Databases for the Web
  • You have access to a server (or servers) running PHP and MySQL. If yuo don't, you can get hold of the software, for free, along with installation instructions at the PHP site and the MySQL site.
  • The server running PHP can connect to the server running MySQL. Test this from the command line first. If you can't connect without PHP, you're not going to be able to connect with PHP. If you're using other servers, you may need to ask the systems administrator for help
  • There's an existing database and table already running on MySQL, which we'll use with the PHP scripts. The database is called first_test, and has a table called people with some data in it. Run the following SQL to create and populate the table:
    mysql> CREATE DATABASE first_test;
    Query OK, 1 row affected (0.31 sec)
    mysql> USE first_test;
    Database changed
    mysql> CREATE TABLE people (
              id int UNIQUE NOT NULL,
    	  first_name varchar(40),
    	  surname varchar(50),
    	  PRIMARY KEY(id)
           );
           Query OK, 0 rows affected (0.24 sec)
    mysql> INSERT INTO people VALUES(1,'Ann','Brache');
    Query OK, 1 row affected (0.09 sec)
    mysql> INSERT INTO people VALUES(2,'Narcizo','Levy');
    Query OK, 1 row affected (0.02 sec)
    mysql> INSERT INTO people VALUES(3,'Tony','Crocker');
    Query OK, 1 row affected (0.00 sec)
    

    Your table should now look as follows:

    mysql> SELECT * FROM people;
    +----+------------+---------+
    | id | first_name | surname |
    +----+------------+---------+
    |  1 | Ann        | Brache  |
    |  2 | Narcizo    | Levy    |
    |  3 | Tony       | Crocker |
    +----+------------+---------+
    3 rows in set (0.19 sec)
    

Connecting

There are many MySQL functions in PHP. Perhaps you've taken a look at the official documentation and been intimidated by what looks like an endless task. But the good news is that to perform basic queries from within MySQL is very easy. This article will show you how to get up and running. Once you're comfortable with the basics, you can start to investigate the other functions, and you'll see that many of them are just duplicate ways of doing the same thing. Let's get started. The first thing to do is connect to the database.
All mysql functions begin with mysql_, so it comes as no surprise that the function to connect to MySQL is called mysql_connect. Let's assume you would connect to MySQL from the server you're running PHP with the following details:

  • Username pee_wee
  • Password let_me_in
From the command line, you'd type the following:
mysql -upee_wee -p
You'd enter the password once the prompt appears (you can also enter it directly on the command line, but get into the habit of doing it this way - it's more secure!). PHP can connect in the same way. The mysql_connect() function looks as follows:

resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

This function returns a resource which is a pointer to the database connection. It's also called a link identifier, or a database handle, and we'll use it in later functions.
Let's replace your connection details, and run this in a script:
<?php
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";	
$dbh = mysql_connect($hostname, $username, $password) 
	or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
// you're going to do lots more here soon
mysql_close($dbh);
?>

All going well, you should see "Connected to MySQL" when you run this script. If you can't connect to the server, make sure your password, username and hostname are correct, and that you've copied the script exactly.

The last line of the script contains another MySQL function - mysql_close(). Although this isn't strictly speaking necessary, PHP will automatically close the connection when the script ends, you should get into the habit of closing what you open. If you start developing more serious applications, or move to other, less tolerant languages, you will find the transition more difficult if you haven't learnt the basics well from the beginning.

Once you've connected, you're going to want to select a database to work with. Let's assume the database is called first_test. To start working in this database (the equivalent of typing USE first_test in MySQL), you'll need the mysql_select_db() function.

bool mysql_select_db ( string database_name [, resource link_identifier])

To change to the first_test database, add the mysql_select_db() function call to your script, as follows:

<?php
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";	
$dbh = mysql_connect($hostname, $username, $password) 
	or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
$selected = mysql_select_db("first_test",$dbh) 
	or die("Could not select first_test");
// you're going to do lots more here soon
mysql_close($dbh);
?>

Go to page: 1  2  3  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives

IT in 2018: Download Free eBook By The Author Of "Does IT Matter?" Simple Registration Is Required.
Download: SQL Backup & DBA Best Practices eBook.
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Webcast: Five Virtualization Trends to Watch. Produced for HP, Citrix, and Intel.
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers. Sponsored by HP, Citrix, and Intel.


Latest Forum Threads
MySQL Forum
Topic By Replies Updated
Transfer information from an access database to MySQl database sculptor44 2 May 14th, 09:50 PM
MERGE STORAGE ENGINE Vs UNION ALL dbnewbie 0 May 14th, 12:16 AM
database sculptor44 0 May 5th, 09:10 PM
arrangeable Database sculptor44 0 May 5th, 08:53 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES