Php Programming Code Examples
Php > Php Classes Code Examples
The program below is intended for database programmers using Visual Basic
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)*/
/* 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 */