gotFusion.com....... Where your adventure begins
Antons Video ProductionsNetObjects Fusion 7 Websites Portal

 


 

On This Page

Connect to DB Server

Connect to Database

Query Database

 Or... What You need on every  page that accesses the MySQL database

In This Tutorial

MySQL--Getting Started

SELECT Statements

Forms

On All Forms

Forms-Add

Forms-Update

Forms-Delete

Combo Boxes

All the Examples on this page can be found in the
php  sample site

The Bare Bones

To access a MySQL database, you must:

  1. Connect to a database server
  2. Connect and open a database connection
  3. Perform a query on that database

To Connect to a Database Server:

$dbconnection = mysql_connect( server address, username, userpassword);

If it was able to connect:

The function mysql_connect returns a "resource ID" (or pointer) to the database server if the connection was able to be made. this ID is stored in the variable $dbconnection.

If it was NOT able to connect to a server:

If the connection was not available, then $dbconnection variable would contain FALSE.

 

Why is this important?

The resource ID may be used to tell future queries and updates to the database which database server connection to use. It is optional in applications with only 1 database server, but is a good practice to use.


Open and Connect to a Database on That Server

$db = mysql_select_db("databasename here", optional db server id here )
// db server id is the resource id from the mysql_connect() statement above

If it was able to open and access the database:

Variable $db will contain a "resource ID" (or pointer) pointing to that database

If it was NOT able to connect

Variable $db will be FALSE.

 

Why is this important?

A database server may have hundreds of separate databases on that single server. this tells the database server WHICH database you wish to connect to. Even your username/password may have access to more than 1 database on a given database server.

Example of connects to Database Server and Database

open the server, database and connect to it

Results in browser:

results of code above


Query the Database

$result = mysql_query("put your database SQL statement here", optional db server id  here);

mysql_query is the primary way of getting information out of your database and putting information back into your database!

What is returned?

What is returned and stored in the variable $result depends on the type of query that mysql_query() performs

for INSERT, UPDATE, DELETE queries, mysql_query() will return the number of rows that were affected by the SQL statement, or it will return FALSE if the SQL statement could not be performed

If the mysql_query() contains a SELECT query, then $result will contain the set of records that the select statement found or it will return FALSE if the query could not be performed.

For other SQL statements, such as CREATE TABLE, DROP TABLE, GRANT, etc., the mysql_query() function will return either TRUE or FALSE based on whether the SQL statement was successful or not.

OK, so I have queried the database, selected some data,
NOW WHAT?????

Well, let's go read about displaying in the browser

next lesson - SELECT statements

View the PREVIOUS page in this tutorial

Return to the TOP of this page

View the NEXT page in this tutorial


|  Fusion  |  Web Design  |  Hosting  |  Resources  |  gotFusion Store  | 

Problems with this page?  

All content copyright © 2002 gotFusion LLC.  The name gotFusion and the gotFusion ® logo are registered trademarks of gotFusion LLC
Copyright, legal notice & privacy statement