WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 3 times)
Viewed : 20698
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  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 */



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
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
Forum with Access 97
Categories : MS Access, Complete Programs, PHP, ODBC, WinNT
Voting Machine with Access 97
Categories : PHP, ODBC, WinNT, MS Access, Complete Programs
These PHP Classes Check if a host is alive using various methods.
Categories : PHP, PHP Classes, Sockets, CURL
Recordset Class for MSSQL database
Categories : PHP Classes, Databases, PHP, MS SQL Server
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
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)
Directory Listing To XML : Outputs XML File of a Given Directory Listing
Categories : PHP, PHP Classes, XML, Directories
Football News Aggregator
Categories : PHP, Object Oriented, PHP Classes, Rich Site Summary (RSS), HTML and PHP
Class to build a select tag in html, useful to build select boxes from a data base
Categories : PHP, HTML and PHP, PHP Classes
A set of functions sitting on top of the abstraction layer that makes it a little easier to do SQL stuff. Documentation is within
Categories : Databases, ODBC, Complete Programs, PHP
Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
Email Class
Categories : PHP, Mail, PHP Classes