PHP Howto
PHP Howto
PHP HOW−TO
Table of Contents
PHP HOW−TO...................................................................................................................................................1
Al Dev (Alavoor Vasudevan) alavoor@yahoo.com ..............................................................................1
1. Introduction..........................................................................................................................................1
2. PHP Download....................................................................................................................................1
3. PHP Tutorial .......................................................................................................................................1
4. IDE tools for PHP ...............................................................................................................................1
5. ctags for PHP ! Surprise!!! ..................................................................................................................1
6. Debugging PHP ..................................................................................................................................1
7. Limitations of PHP..............................................................................................................................1
8. Related URLs.......................................................................................................................................1
9. Other Formats of this Document..........................................................................................................1
10. Copyright...........................................................................................................................................1
11. Appendix A Database Wrapper Example .........................................................................................1
12. Appendix B SQL abstraction Example .............................................................................................1
13. Appendix C PostgreSQL large object Example................................................................................2
14. Appendix D User authentication Example .......................................................................................2
15. Appendix E Network admin Example ..............................................................................................2
16. Appendix F PostgreSQL Database Wrapper Examples....................................................................2
17. Appendix G Microsoft SQL Server DB Wrapper Example .............................................................2
18. Appendix H Sybase SQL Server DB Wrapper Example ..................................................................2
19. Appendix I phpDB.inc Example .......................................................................................................2
20. Appendix J phpDBTest.php3 Example.............................................................................................2
1. Introduction..........................................................................................................................................2
2. PHP Download...................................................................................................................................3
2.1 PHP Installation................................................................................................................................3
3. PHP Tutorial ......................................................................................................................................3
4. IDE tools for PHP ..............................................................................................................................5
5. ctags for PHP ! Surprise!!! .................................................................................................................5
6. Debugging PHP .................................................................................................................................8
7. Limitations of PHP............................................................................................................................10
8. Related URLs.....................................................................................................................................11
9. Other Formats of this Document........................................................................................................11
10. Copyright.........................................................................................................................................13
11. Appendix A Database Wrapper Example ......................................................................................13
12. Appendix B SQL abstraction Example ..........................................................................................18
13. Appendix C PostgreSQL large object Example.............................................................................22
14. Appendix D User authentication Example ....................................................................................22
15. Appendix E Network admin Example ...........................................................................................23
16. Appendix F PostgreSQL Database Wrapper Examples.................................................................25
17. Appendix G Microsoft SQL Server DB Wrapper Example ..........................................................32
18. Appendix H Sybase SQL Server DB Wrapper Example ...............................................................39
19. Appendix I phpDB.inc Example ....................................................................................................46
20. Appendix J phpDBTest.php3 Example..........................................................................................47
i
PHP HOW−TO
Al Dev (Alavoor Vasudevan) alavoor@yahoo.com
v7.0, 10 Jul 2000
This document tells you howto develop PHP programs and also to migrate all the Windows 95 GUI
applications to powerful PHP + HTML + DHTML + XML + Java applets + Javascript. The information in
this document applies to all the operating sytems where PHP is ported that is − Linux, Windows 95/NT,
OS/2, all flavors of Unix like Solaris, HPUX, AIX, SCO, Sinix, BSD, etc..
1. Introduction
2. PHP Download
• 2.1 PHP Installation
3. PHP Tutorial
4. IDE tools for PHP
5. ctags for PHP ! Surprise!!!
6. Debugging PHP
7. Limitations of PHP
8. Related URLs
9. Other Formats of this Document
10. Copyright
11. Appendix A Database Wrapper Example
12. Appendix B SQL abstraction Example
PHP HOW−TO 1
PHP HOW−TO
1. Introduction
PHP stands for 'Hypertext Pre−Processor' and is a server side HTML scripting/programming language. PHP
is a tool that lets you create dynamic web pages. PHP−enabled web pages are treated just like regular HTML
pages and you can create and edit them the same way you normally create regular HTML pages.
PHP was kept the "top secret and strictly confidential" computer language by many companies in the
world, but now had become the most well−known and most widely used scripting language for web, internet,
e−commerce and business−to−business projects. Even today many competing companies keep PHP language
as a highly confidential matter not disclosing to outsiders (competitors).
PHP will storm the entire world and will take the IT industry by surprise!! The power of PHP is that it is
cross−platform and runs everywhere!! It runs on Linux, Windows 95/98/NT, Windows 2000, Solaris,
HPUX and all flavors of unix. PHP is write once and deploy anywhere and everywhere. It runs on many
web−servers like Apache, Microsoft IIS, etc..
PHP runs 5 to 20 times faster than Java!! It is extremely easy to use and you can develop very complex
web/e−commerce applications very rapidly in a very short period of time.
It has object oriented features and takes the best features from Java, C++, PERL and "C" langauges. PHP
language is a marriage of best features from Java, C++, PERL and C.
PHP is the real gem of all the scripting/programming languges and will soon become the "MECCA" for
programmers world−wide!! PHP has a huge user base and a large developer base as it runs on both
window95/NT and all flavors of unixes.
PHP can be compiled and optimized to make it run even faster by using the Zend Optimizer. Zend optimizer
is integrated with PHP in PHP version 4.0.
You would normally use a combination of PHP (70% code) + HTML/DHTML/XML (25% code) +
Javascript (5% code client side validations) for your e−commerce projects.
2. PHP Download
• PHP main site http://www.php.net
• PHP resources http://ils.unc.edu/web−db/php/links.html
• PHP Code Exchange − http://px.sklar.com
3. PHP Tutorial
In this tutorial we assume that your server has support for PHP activated and that all files ending in .php3 are
handled by PHP.
Your first PHP−enabled page: Create a file named hello.php3 and in it put the following lines:
Note that this is not like a CGI script. Think of it as a normal HTML file which happens to have a set of
special tags available to you.
If you tried this example and it didn't output anything, chances are that the server you are on does not have
PHP enabled. Ask your administrator to enable it for you.
The point of the example is to show the special PHP tag format. In this example we used < ?php to indicate
the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ? > .
You may jump in and out of PHP mode in an HTML file like this all you want.
We are going to check what sort of browser the person viewing the page is using. In order to do that we check
the user agent string that the browser sends as part of its request. This information is stored in a variable.
Variables always start with a dollar−sign in PHP. The variable we are interested in is
$HTTP_USER_AGENT. To display this variable we can simply do:
2. PHP Download 3
PHP HOW−TO
For the browser that you are using right now to view this page, this displays:
There are many other variables that are automatically set by your web server. You can get a complete list of
them by creating a file that looks like this:
<?php phpinfo()?>
Then load up this file in your browser and you will see a page full of information about PHP along with a list
of all the variables available to you.
You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just
a single echo.
<?php
if(strstr($HTTP_USER_AGENT,"MSIE")) {
echo "You are using Internet Explorer<br>";
}
?>
We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a
PHP block:
<?php
if(strstr($HTTP_USER_AGENT,"MSIE"))
{
?>
< center>< b>You are using Internet Explorer< /b>< /center>
<?
}
else
{
?>
< center>< b>You are not using Internet Explorer< /b>< /center>
<?
}
?>
Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent
straight HTML. The important and powerful point to note here is that the logical flow of the script remain
intact. Only one of the HTML blocks will end up getting sent to the viewer. Running this script right now
results in:
2. PHP Download 4
PHP HOW−TO
One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is
important to understand is that any form element in a form will automatically result in a variable with the
same name as the element being created on the target page. This probably sounds confusing, so here is a
simple example. Assume you have a page with a form like this on it:
There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When
the user fills in this form and hits the submit button, the action.php3 page is called. In this file you would
have something like this:
Hi <?php echo $name?>. You are <?php echo $age?> years old.
Surprise!! The $name and $age variables are automatically set for you by PHP !!
The ptags program for PHP is given below, which you can use to create the tags for PHP source code. Your
productivity will improve 3 to 4 times if you use ptags.
See also Vim color text editor for PHP, C, C++ at http://metalab.unc.edu/LDP/HOWTO/Vim−HOWTO.html
system(buff);
fclose(fp);
*/
FILE *fpin = NULL;
fpin = fopen(argv[ii], "r");
if (fpin == NULL)
{
cerr << "\nError opening file : " << argv[ii] << endl;
exit(−1);
}
char buff[BUFF_LEN + 100];
while (isspace(*dd))
dd++;
return dd;
}
6. Debugging PHP
To debug PHP programs create a file "debug2.inc" having the following functions :
<?php
6. Debugging PHP 8
PHP HOW−TO
?>
In your PHP source code initial page which is generally index.php3, put a line like
<?php
include ("debug2.inc");
init_debug_file();
// all other commands follows here ...
// ...........
?>
To output debug values, in your PHP source code files, put debug2_() calls as illustrated below:
<?php
include ("debug2.inc");
debug2_(__FILE__, __LINE__, "f_somevariable", $f_somevariable);
function aa()
{
$aa = 8;
debug2_(__FILE__, __LINE__, "aa", $aa);
}
6. Debugging PHP 9
PHP HOW−TO
?>
When you run the PHP program the output will be traced in the file called debug.out giving the filename,
linenumber, variable name and it's value.
Use the debug2_() generously in your code. The usage of debug2_() calls in your program will NOT have
any impact on the final production code and also has no impact on the performance because they will be
filtered out as described below. You can use copy and paste to save time of typing debug2() calls or use the
'yank to buffer' feature of Vi editor and paste.
When you are done development and testing and when you are ready to deploy on the production server,
filter out the debug2_ calls from your source code. At unix prompt −
And now copy the files from production to the deployment area.
7. Limitations of PHP
Everything has limitations or disadvantages and PHP is no exception. The following are the limitations of
PHP (so be WARNED !!)
1. PHP is NOT 100 % pure Object Oriented scripting language. PHP is good if your PHP code size does
not exceed 3,00,000 lines. Maintainence of PHP code greater than 1,00,000 lines becomes more
difficult.
2. PHP will NOT give the performance of "C" or "C++" language. Because it is scripting language and
is interpreted it will be a bit slower than the optimized "C++" programs. For top performance, you
should use "C++" and fast−CGI with database/webserver connection pooling and use C++ compiler
optimizer "−O3" options. Zend optimizer in PHP 4 will speed up the performance of PHP to certain
extent.
On the other hand, PHP has lot of advantages and it's advantages outweigh it's limitations −
1. You can very rapidly develop web applications in PHP as compile and link is eliminated in PHP
scripting language.
2. PHP applications are very stable and do not depend on the browser technologies unlike Javascript
7. Limitations of PHP 10
PHP HOW−TO
applications which depend on browsers. PHP will give you the freedom to select any server platform
and browser does not know that the HTML page is generated by PHP!!
3. PHP has excellent database conectivity to all SQL database servers.
4. PHP has partial support for Object oriented features
5. PHP has C++, Perl, Javascript like syntax features and has programs like 'ptags/ctags' to navigate the
source code
6. PHP has Zend optimizer which speeds up the performance
7. PHP runs on all unixes, linux, Windows 95/NT/2000 and is more powerful than ASP, JSP and others.
8. PHP has a very large user base and developer base.
WARNING: If you want 100% pure Object Oriented scripting language than you MUST consider Python.
The 'Python' is a object oriented scripting language from ground up. You would be using the Python Web
Application server called 'Zope' which is available at − http://www.zope.org and python is at
8. Related URLs
Visit following locators which are related to C, C++ −
• You can get this HOWTO document as a single file tar ball in HTML, DVI, Postscript or SGML
formats from − ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/other−formats/
• Plain text format is in: ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO
• Translations to other languages like French, German, Spanish, Chinese, Japanese are in
ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO Any help from you to translate to other languages is
welcome.
The document is written using a tool called "SGML−Tools" which can be got from −
http://www.sgmltools.org Compiling the source you will get the following commands like
• sgml2html CVS−HOWTO.sgml (to generate html file)
• sgml2rtf CVS−HOWTO.sgml (to generate RTF file)
• sgml2latex CVS−HOWTO.sgml (to generate latex file)
LaTeX documents may be converted into PDF files simply by producing a Postscript output using
sgml2latex ( and dvips) and running the output through the Acrobat distill ( http://www.adobe.com)
command as follows:
8. Related URLs 11
PHP HOW−TO
Or you can use Ghostscript command ps2pdf. ps2pdf is a work−alike for nearly all the functionality of
Adobe's Acrobat Distiller product: it converts PostScript files to Portable Document Format (PDF) files.
ps2pdf is implemented as a very small command script (batch file) that invokes Ghostscript, selecting a
special "output device" called pdfwrite. In order to use ps2pdf, the pdfwrite device must be included in the
makefile when Ghostscript was compiled; see the documentation on building Ghostscript for details.
• http://sunsite.unc.edu/LDP/HOWTO/CVS−HOWTO.html
Also you can find this document at the following mirrors sites −
• http://www.caldera.com/LDP/HOWTO/CVS−HOWTO.html
• http://www.WGS.com/LDP/HOWTO/CVS−HOWTO.html
• http://www.cc.gatech.edu/linux/LDP/HOWTO/CVS−HOWTO.html
• http://www.redhat.com/linux−info/ldp/HOWTO/CVS−HOWTO.html
• Other mirror sites near you (network−address−wise) can be found at
http://sunsite.unc.edu/LDP/hmirrors.html select a site and go to directory
/LDP/HOWTO/CVS−HOWTO.html
In order to view the document in dvi format, use the xdvi program. The xdvi program is located in
tetex−xdvi*.rpm package in Redhat Linux which can be located through ControlPanel | Applications |
Publishing | TeX menu buttons. To read dvi document give the command −
And resize the window with mouse. To navigate use Arrow keys, Page Up, Page Down keys, also you can
use 'f', 'd', 'u', 'c', 'l', 'r', 'p', 'n' letter keys to move up, down, center, next page, previous page etc. To turn off
expert menu press 'x'.
You can read postscript file using the program 'gv' (ghostview) or 'ghostscript'. The ghostscript program is in
ghostscript*.rpm package and gv program is in gv*.rpm package in Redhat Linux which can be located
through ControlPanel | Applications | Graphics menu buttons. The gv program is much more user friendly
than ghostscript. Also ghostscript and gv are available on other platforms like OS/2, Windows 95 and NT,
you view this document even on those platforms.
• Get ghostscript for Windows 95, OS/2, and for all OSes from http://www.cs.wisc.edu/~ghost
gv howto.ps
8. Related URLs 12
PHP HOW−TO
ghostscript howto.ps
You can read HTML format document using Netscape Navigator, Microsoft Internet explorer, Redhat Baron
Web browser or any of the 10 other web browsers.
You can read the latex, LyX output using LyX a X−Windows front end to latex.
10. Copyright
Copyright policy is GNU/GPL as per LDP (Linux Documentation project). LDP is a GNU/GPL project.
Additional requests are − Please retain the author's name, email address and this copyright notice on all the
copies. If you make any changes or additions to this document then you please intimate all the authors of this
document.
<?php
if ($dbObjDefined != 1)
{
$dbObjDefined = 1;
if(isset($parame[user]))
10. Copyright 13
PHP HOW−TO
$user = $parame[user];
else
$user = $this−>user;
if(isset($parame[password]))
$password = $parame[password];
else
$password = $this−>password;
if(isset($parame[port]))
$port = $parame[port];
else
$port = $this−>port;
if(isset($parame[dbname]))
$dbname = $parame[dbname];
else
$dbname = $this−>dbname;
if ($this−>resultset)
{
$recset = new recordset;
$recset−>init($this−>resultset);
return $recset;
}
else
{
return 0;
}
}
function valid()
{
return $this−>resultset;
}
/*
** This is a simple recordset class which can be
** traversed using next(), prev(), and current() methods.
** It is initialized from a resultset returned from the
** function "pg_Exec" or can be generated by a call to the
** exec method from the dbObj class given above.
** Below "Tuples" means rows.
*/
class recordset
10. Copyright 14
PHP HOW−TO
{
var $resultset;
var $index;
var $numFields;
var $numTuples;
function init($newResultset)
{
$this−>resultset = $newResultset;
$this−>index = 0;
$this−>numFields = pg_NumFields($this−>resultset);
$this−>numTuples = pg_NumRows($this−>resultset);
}
10. Copyright 15
PHP HOW−TO
function valueof($col)
{
if ($col < $this−>numFields)
{
return pg_Result($this−>resultset, $this−>index, $col);
}
else
{
return "";
}
}
// Decrement index
function prev()
{
if ($this−>index >= 0)
{
$this−>index−−;
10. Copyright 16
PHP HOW−TO
return 1;
}
else
{
return 0;
}
}
function last()
{
$this−>index = $this−>numTuples −1 ;
}
function display()
{
if (!$this−>valid() )
{
return;
}
printf( "<tr>\n");
10. Copyright 17
PHP HOW−TO
$this−>first();
while (!$this−>eof())
{
printf( "<tr>\n");
?>
/*
* SAL − SQL Abstraction Library
* version 0.01
*/
/*
** Set the variable $dbtype to any of the following
** values: MySQL, mSQL, Postgres, ODBC before including this library
*/
// $dbtype = "MySQL";
// $dbtype = "mSQL";
// $dbtype = "PostgreSQL";
// $dbtype = "ODBC";
switch ($dbtype)
{
case "MySQL":
$conn=mysql_pconnect($host, $user, $password);
mysql_select_db($db);
return $conn;
break;;
case "mSQL":
$conn=msql_pconnect($host);
msql_select_db($db);
return $conn;
break;;
case "PostgreSQL":
$conn=pg_pconnect($host, "5432", "",$db);
return $conn;
break;;
case "ODBC":
$conn=odbc_pconnect($db,$user,$password);
return $conn;
break;;
default:
$conn=mysql_pconnect($host, $user, $password);
mysql_select_db($db);
return $conn;
break;;
}
}
case "mSQL":
$res=msql_query($query, $id);
return $res;
break;;
case "PostgreSQL":
$res=pg_exec($id,$query);
return $res;
break;;
case "ODBC":
$rid=odbc_prepare($id,$query);
$res=odbc_execute($rid);
return $res;
break;;
default:
$res=mysql_query($query, $id);
return $res;
break;;
}
}
switch ($dbtype)
{
case "MySQL":
$rows=mysql_num_rows($res);
return $rows;
break;;
case "mSQL":
$rows=msql_num_rows($res);
return $rows;
break;;
case "PostgreSQL":
$rows=pg_numrows($res);
return $rows;
break;;
case "ODBC":
$rows=odbc_num_rows($res);
return $rows;
break;;
default:
$rows=mysql_num_rows($res);
return $rows;
break;;
}
}
// SQL_fetchrow($res,$row)
// given a result identifier, returns an array with the resulting row
// Needs also a row number for compatibility with PostgreSQL
function SQL_fetch_row($res, $nr)
{
global $dbtype;
switch ($dbtype)
{
case "MySQL":
$row = array();
$row = mysql_fetch_row($res);
return $row;
break;;
case "mSQL":
$row = array();
$row = msql_fetch_row($res);
return $row;
break;;
case "PostgreSQL":
$row = array();
$row = pg_fetch_row($res,$nr);
return $row;
break;;
case "ODBC":
$row = array();
$cols = odbc_fetch_into($res, $nr, &$row);
return $row;
break;;
default:
$row = array();
$row = mysql_fetch_row($res);
return $row;
break;;
}
}
// SQL_fetch_array($res,$row)
// given a result identifier, returns an associative array
// with the resulting row using field names as keys.
// Needs also a row number for compatibility with PostgreSQL.
function SQL_fetch_array($res, $nr)
{
global $dbtype;
switch ($dbtype)
{
case "MySQL":
$row = array();
$row = mysql_fetch_array($res);
return $row;
break;;
case "mSQL":
$row = array();
$row = msql_fetch_array($res);
return $row;
break;;
case "PostgreSQL":
$row = array();
$row = pg_fetch_array($res,$nr);
return $row;
break;;
/*
* ODBC doesn't have a native _fetch_array(), so we have to
* use a trick. Beware: this might cause HUGE loads!
*/
case "ODBC":
$row = array();
$result = array();
<?
$database = pg_Connect ( "", "", "", "", "jacarta");
pg_exec ($database, "BEGIN");
$oid = pg_locreate ($database);
echo ( "$oid\n");
$handle = pg_loopen ($database, $oid, "w");
echo ( "$handle\n");
pg_lowrite ($handle, "foo");
pg_loclose ($handle);
pg_exec ($database, "COMMIT");
pg_close ($database);
?>
From the PHP 3 Manual: Works only if PHP is an Apache module. Instead of simply printing out the
$PHP_AUTH_USER and $PHP_AUTH_PW, you would probably want to check the username and password
for validity. Perhaps by sending a query to a database, or by looking up the user in a dbm file.
<?php
if (!$PHP_AUTH_USER)
{
Header("WWW−authenticate: basic realm=\"My Realm\"");
Header("HTTP/1.0 401 Unauthorized");
echo "Text to send if user hits Cancel button\n";
exit;
}
else
{
echo "Hello $PHP_AUTH_USER.<P>";
echo "You entered $PHP_AUTH_PW as your password.<P>";
}
?>
As a web−developer, you're probably used to such lovely tools as ping, whois, nslookup etc. But what when
you need one of those utilities at a client's office and have no access to telnet? Good guess. Time to look up
the functions in the "Network" section of the PHP manual.
Socket operations:
The most important function there is fsockopen(). Using this function, you can connect to any open port on a
server and establish a socket connection with it. The function's syntax is as following:
The first two arguments are obvious, the next two are optional and used for error handling. The "errno" and
"errstr" should be passed by reference. "Passing by reference" means that the original variable will get
modified. Normally, the content of a variable passed to a function wouldn't be modified.
So, you could use this function to open a connection to a webserver and print out the headers:
In this example you see that you can apply any file operations (fread, fwrite etc) to the the pointer you got
using the fsockopen() call. Note that the example realizes a HTTP/1.0 client − it won't work with
name−based virtual hosts.
Finger: Naturally, you can also open connections to other ports. Writing a small finger client with PHP is
trivial therefore. Let's change the example from above to query a finger daemon:
Blocking and non−blocking operations: But there's a problem with all those functions. They work fine if
Modifying these 3 functions to use non−blocking socket calls is left as an exercise for you.
Description: A PHP database wrapper for various database servers with a powerful Recordset for result data
manipulation. Database results are flushed automatically by phpDB.
To get this file, in the web−browser, save this file as 'Text' type as phpDB−postgresql.lib
<?php
/*
Name: phpDB PostgreSQL module
Version: 1.02bR6
Description: A PHP database wrapper for various database
servers with a powerful recordset for result data
manipulation. Database results are flushed
automatically by phpDB.
*/
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Class Name: phpDB
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
class phpDB
{
/* public variables */
var $version = '1.02bR6'; // Version number of phpDB
// This variable keeps what database type is going to
// be used. Current supported database server are
// MySQL, MSQL, SQL Server, and Sybase
var $databaseType = '';
// Specifies which database is going to be used
var $databaseName = '';
// The hostname of the database server, port
// number is optional. e.g: "db.devNation.com"
var $hostname = '';
var $username = ''; // used to connect to the database server
var $password = ''; // Password for the username
}
if ($this−>password) {
$connString .= " password=$this−>password";
}
$connString .= " dbname=$this−>databaseName";
$this−>_connectionID = @pg_Connect($connString);
return $this−>_connectionID;
}
$this−>_connectionID = @pg_pConnect($connString);
if ($this−>_connectionID) {
$this−>_isPersistentConnection = true;
}
return $this−>_connectionID;
}
return false;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Class Name: Recordset
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
class Recordset
{
/* public variables */
var $fields;
// indicates that the current record position is before
// the first record in a Recordset object
var $BOF = null;
/* private variables */
var $_numOfRows = −1; // NEVER change the value! READ−ONLY!
var $_numOfFields = −1; // NEVER change the value! READ−ONLY!
// Holds anything that was returned from the database specific functions
var $_tempResult = '';
// This variable keeps the result link identifier
var $_queryID = −1;
// This variable keeps the current row in the Recordset
var $_currentRow = −1;
}
}
?>
Description: A PHP database wrapper for various database servers with a powerful Recordset for result data
manipulation. Database results are flushed automatically by phpDB.
To get this file, in the web−browser, save this file as 'Text' type as phpDB−mssql.lib
<?php
/*
Name: phpDB Microsoft SQL Server module
Version: 1.02bR6
Description: A PHP database wrapper for various
database servers with a powerful
Recordset for result data manipulation. Database
results are flushed automatically by phpDB.
*/
// Define this module, to prevent double class declaration
if (!defined("_PHPDB_ABSTRACT_LAYER"))
{
define("_PHPDB_ABSTRACT_LAYER", 1 );
}
else
return;
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Class Name: phpDB
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
class phpDB
{
// public variables
var $version = '1.02bR6'; // Version number of phpDB
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Class Name: Recordset
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
class Recordset
{
/* public variables */
var $fields;
// indicates that the current record position is
// before the first record in a Recordset object
var $BOF = null;
// indicates that the current record position is
// after the last record in a Recordset object
var $EOF = null;
// Private variables
var $_numOfRows = −1; // NEVER change the value! READ−ONLY!
var $_numOfFields = −1; // NEVER change the value! READ−ONLY!
{
if ($this−>getNumOfRows() > 0) {
$this−>fields = array();
$this−>_currentRow = 0;
if (@mssql_data_seek($this−>_queryID, $this−>_currentRow)) {
$this−>fields = @mssql_fetch_array($this−>_queryID);
$this−>EOF = false;
/* This is not working. True all the time */
if ($this−>fields) {
return true;
}
}
}
$this−>EOF = true;
return false;
}
Description: A PHP database wrapper for various database servers with a powerful Recordset for result data
manipulation. Database results are flushed automatically by phpDB.
To get this file, in the web−browser, save this file as 'Text' type as phpDB−sybase.lib
<?php
/*
Name: phpDB Sybase module
Version: 1.02bR6
Description: A PHP database wrapper for various database
servers with a powerful Recordset for result data
manipulation. Database results are flushed
automatically by phpDB.
*/
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Class Name: phpDB
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
class phpDB
{
/* public variables */
var $version = '1.02bR6'; // Version number of phpDB
// This variable keeps what database type is going
// to be used. Current supported database server
// are MySQL, MSQL, SQL Server, and Sybase
var $databaseType = '';
// Specifies which database is going to be used
var $databaseName = '';
// The hostname of the database server, port number
// is optional. e.g: "db.devNation.com"
if ($argDatabaseName != "") {
$this−>databaseName = $argDatabaseName;
}
else {
/* No database selected */
return false;
}
}
function errorMsg() {
$this−>_errorMsg = "errorMsg() is not available for Sybase";
return $this−>_errorMsg;
}
function close() {
if ($this−>_queryIDList && sizeof($this−>_queryIDList > 0)) {
while(list($_key, $_resultID) = each($this−>_queryIDList)) {
@sybase_free_result($_resultID);
}
}
// If its not a persistent connection, then
// only the connection needs to be closed
if ($this−>_isPersistentConnection != true) {
return @sybase_close($this−>_connectionID);
}
else {
return true;
}
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Class Name: Recordset
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
class Recordset
{
/* public variables */
var $fields;
// indicates that the current record position is
return $this−>_tempResult;
}
function getNumOfRows() {
return $this−>_numOfRows;
}
function getNumOfFields() {
return $this−>_numOfFields;
}
Description: A PHP database wrapper for various database servers with a powerful Recordset for result data
manipulation. Database results are flushed automatically by phpDB.
To get this file, in the web−browser, save this file as 'Text' type as phpDB.inc
<?php
/*
Name: phpDB General module
Version: 1.02bR6
Description: A PHP database wrapper for various
database servers. Database results are flushed
automatically by phpDB. Supported database
servers are MySQL, MSQL, PostgreSQL, Microsoft
SQL Server and Sybase.
*/
if (!defined("_PHPDB_GENERAL_LAYER")) {
define("_PHPDB_GENERAL_LAYER", 1 );
}
else
return;
case "msql":
case "postgresql":
case "mssql":
case "sybase":
case "informix":
include("$phpDBRootPath". "/phpDB−" . "$dbType.lib");
break;
case "":
die("Please edit phpDB.inc in order to use phpDB");
return false;
default:
die("Invalid database selection");
return false;
}
return true;
}
useDB($databaseType);
?>
Description: A PHP database wrapper for various database servers with a powerful Recordset for result data
manipulation. Database results are flushed automatically by phpDB.
To get this file, in the web−browser, save this file as 'Text' type as phpDB−mssql.lib
<html>
< head>
< title>Untitled< /title>
< /head>
< body>
<?php
// Assumed this file is placed in the same directory with phpDB.inc
include("phpDB.inc");
$db = new phpDB();
$db−>pconnect("hostName", "userName", "passWord", "databaseName") or die ("Can't connect
$rs = $db−>execute("SELECT * FROM Items");
$numOfRows = $rs−>getNumOfRows();
echo "Number of Rows: $numOfRows";
$rs−>close();
$db−>close(); // optional
?>
< /body>
< /html>