WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : php for odbc /* connect to access from odbc */
Categories : PHP Classes, ODBC, MS Access, PHP Update Picture
Santiago Calo
Date : May 16th 2001
Grade : 2 of 5 (graded 2 times)
Viewed : 10357
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Santiago Calo
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

The program below is intended for database programmers using Visual Basic and who
uses ODBC to connect on a database.

During my first season with PHP, it was really a learning curve for me, especially when i
access my database and i really had fun, but i find it hard typing so many commands over and
over again so I thought maybe I could make it more easier by creating database objects.


Now onto our PHP Database Objects codes.

Open a notepad.exe or any editor you wish to make your scripts with. I use notepad.exe.
Copy the codes and save your file as phpdbo.php (which is stands for "PHP Database
Objects").

<?php

/*-------------------------------------------------------------- */
/* PHP DATABASE OBJECTS (PHPDBO.PHP) */
/*-------------------------------------------------------------- */
/* Author : Santiago B. Calo III */
/* Email : myownphp@yahoo.com */
/* As of now, all of the codes below support ODBC only, */
/* but you can modify my codes in order to fit your needs */
/* */
/* note: if you use this code, don't omit this header */
/*-------------------------------------------------------------- */


/* Create an class for database connectivity */
class odbc {
/* Set connection variables */
var $record_set,$con_nect,$cur_sur,$dbtype,$num_rows,$current_rec,$cur_field,
$current_row;

/* Create an object to set the cursor of the database */
function cursorname($cursor){
$this->cur=$cursor;
}

/*-----------------------------------*/
/* Connects to the remote database */
/* $domain = data source name */
/* $userid = user id */
/* $passwd = password */
/* $cursr = type of cursor */
/*-----------------------------------*/

/* Create an object to connect to the database/ODBC */
function opendb($domain,$userid,$passwd,$cursr){
$this->con_nek = odbc_connect($domain,$userid,$passwd,$cursr); /* means
odbc_connect($dsn,$uid,$pwd,$cur) */
if (!$this->con_nek){
return $this->con_nek;
break;
}
else{
return $this->con_nek;
}
}

/*----------------------------------------- */
/* execute an sql query for the recordset */
/* $SqlQuery = the sql statement */
/*----------------------------------------- */

function openrs($sqlstatement){

$this->record_set = odbc_exec($this->con_nek,$sqlstatement); /* get the result */
while (odbc_fetch_row($this->record_set)){
$this->num_rows += 1; /* count the number of rows */
}
$this->current_rec = odbc_fetch_row($this->record_set,1); /* gets the current row */
$this->current_row = 1; /* reset row (first row) */
return $this->record_set;
}

/*-----------------------------------*/
/* display the value of the field */
/* $dbtype = sql server type */
/* $kword = column name */
/*-----------------------------------*/

function getfield($kword){
$this->field = odbc_result($this->record_set,$kword); /* get the value of the column
*/
return $this->field; /* return the value */
}

// move to the first record
function movetofirst(){
$this->current_row=1;
$this->current_rec = odbc_fetch_row($this->record_set,1);
}

// move to the next record
function movetonext(){
$this->current_row+=1; /* move to the next row */
if ($this->current_row <= $this->num_rows){ /* see if current row is less than the
number of rows returned */
$this->current_rec = odbc_fetch_row($this->record_set,$this->current_row); /* set
current row */
}
}


// move to the previous record
function movetoprev(){
$this->current_row-=1; /* move to the previous row */
if (!$this->current_row < 1){ /* see if current row is greater than 0 */
$this->current_rec = odbc_fetch_row($this->record_set,$this->current_row); /* set
current row */
}
}


// move to the last record
function movetolast(){
$this->current_row = $this->num_rows; /* move to the last row */
$this->current_rec = odbc_fetch_row($this->record_set,$this->current_row); /* set
current row */
}

function eof(){
if ($this->current_row > $this->num_rows) /* see if last row is reached */
{
$this->current_row = $this->num_rows; /* if current row passes the number of rows,
set it back to the last row */
return true; /* returns true if current row is the last row */
}
else
{
return false; /* returns false if current row is less than the number of rows */
}
}

function bof(){
if ($this->current_row < 1) /* see if first row is reached */
{
$this->current_row = 1; /* if current row passes the first row, set it back to the first row
*/
return true; /* returns true if current row is the first row */
}
else
{
return false;/* returns false if current row is not yet the first row */
}
}

function close(){
odbc_close($this->con_nek);
}
function free(){
odbc_free_result($this->record_set);
}

}

?>


Contratulations, you now have in your posession a kewl tool to access ODBC database the
Object Oriented Way.

Let's try out the PHPDBO.PHP


Creating the php script


First of all we must include our PHPDBO since it is stored in another filename. You may use
include() or require(),
currently I am using require().


Using the PHPDBO.PHP



<?php

require('phpdbo.php'); /* include the PHP Database Objects */


$my_odbc = new odbc; /* create object connection */

$dbtype="odbc";
$dsn="My ODBC";
$usr="admin";
$pwd="";
$cursr="SQL_CUR_DEFAULT";

$my_odbc->opendb($dbtype,$dsn,$usr,$pwd,$cursr); /* connect to database */


/* set sql statement */
$my_sql = "select * from database";

/* get result from database */
$my_odbc->openrs($my_odbc,$my_sql);

$my_odbc->movetofirst(); /* move to first row */

/* display all fields */
while (!$my_odbc->eof()){
print $my_odbc->getfield("author"); /* displays the field */
print "<br>";
$my_odbc->movetonext();
}

print "<p> note the fields above if the
movetonext(),
movetolast(),
movetoprev(),
movetofirst(),
corresponds below : ";

print "<p> move to first row<br>";
$my_odbc->movetofirst();
print $my_odbc->getfield("author");

print "<p>move to last row<br>";
$my_odbc->movetolast();
print $my_odbc->getfield("author");

print "<p> move to prev row<br>";
$my_odbc->movetoprev();
print $my_odbc->getfield("author");

print "<p> move to first row<br>";
$my_odbc->movetofirst();
print $my_odbc->getfield("author");

print "<p> move to next row<br>";
$my_odbc->movetonext();
print $my_odbc->getfield("author");

print "<p> move to prev row<br>";
$my_odbc->movetoprev();
print $my_odbc->getfield("author");
print "<p>";


$my_odbc->free() /* clear out the memory */
$my_odbc->close(); /* close connection */

?>


Thats all folks, enjoy and have a nice coding =). Some features will be added shortly.



Example
/* display all fields */

while (!$my_odbc->eof()){
print $my_odbc->getfield("author"); /* displays the field */
print "<br>";
$my_odbc->movetonext();
}


/*note : connect to access database only.... mysql is still under contruction */



Voting Machine with Access 97
Categories : PHP, ODBC, WinNT, MS Access, Complete Programs
Forum with Access 97
Categories : MS Access, Complete Programs, PHP, ODBC, WinNT
ADODB Database Wrapper Abstraction Library for PHP: MySQL, MSSQL, Oracle, Interbase,ODBC, Microsoft Access and FoxPro.
Categories : PHP Classes, Databases, PHP, General SQL, ODBC
Recordset Class like ADO Recordset (plus DataBase Splitting feature) using ODBC functions
Categories : PHP Classes, ODBC, Databases, PHP
Writing Portable MySQL Code in PHP: Porting to Oracle, Microsoft SQL Server, Sybase, Interbase, PostgreSQL and other databases using ADODB class library.
Categories : MySQL, PHP, PHP Classes, ODBC, General SQL
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
Authorize.net AIM Interface Class v1.0.0
Categories : PHP, PHP Classes, Ecommerce, Payment Gateways
A simple class with some HTML output functions that would come in handy for consistent page layout etc.
Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
News management class
Categories : PHP, PHP Classes, Beginner Guides
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself.
Categories : PHP, PHP Classes, Templates, Complete Programs
Simple Template Class/Example
Categories : PHP, Templates, PHP Classes
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
MS Word Mail Merge Automation (COM)
Categories : PHP, PHP Classes, COM