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 : This is a class with functions that are taken from simple SQL statements. I made it to have an easier connection between PHP3 and DBase files.
Categories : General SQL, PHP, PHP Classes, Databases Update Picture
Steven Apostolou
Date : Aug 28th 1998
Grade : 1 of 5 (graded 1 times)
Viewed : 5389
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Steven Apostolou
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php

/* Writen by Mythos Internet Design (S.A. Apostolou)

****
This class is provided with no guarantees that it will either
work properly or not trash your machine. So don't blame me if
it does. It works great for me though, and you can do whatever
you want to it in the name of free software.
****

I wanted to have a sort of SQL connection with the dbase connectivity of PHP3.
The functions in this class are like simple SQL statements.

select_all = SELECT * FROM table
select_fields = SELECT field1, field2, ect. FROM table
select_all_where_values = SELECT * FROM table WHERE values
select_fields_where_values = SELECT field1, field2, ect. FROM table WHERE
values
delete_rows_where_values = DELETE FROM table WHERE values


Functions:

connect (string filename) // Connect to a
dbase table
close () // Close the
connection with the dbase table
num_rows () // Returns the
number of records (rows) in the dbase table
fields_num () // Returns the
number of fields (collums) in the dbase table

create_table (string filename, array fields) // Creates a new
dbase table and connects to it
insert_values (array values) // Add values (a
record) to the dbase table
delete_rows_where_values (array array-values) // Delete row(s)
where specified values are in

select_all () // Returns a two-
dimensional array with all the rows and with all the fields of the dbase table
select_fields (array fields) // Returns a two-
dimensional array with all the rows and with all the specified fields of the
dbase table
select_all_where_values (array array-values) // Returns a two-
dimensional array with all the fields and with all rows which are filtered on
specified values in the rows of the dbase table
select_fields_where_values (array fields, array array-values) // Returns a two-
dimensional array with all the specified fields and with all rows which are
filtered on specified values in the rows of the dbase table

result_current () // Returns a
HTML-table with the last select function that was done on the dbase table
result_all () // Returns a
HTML-table of the complete dbase table

Array fields is an array like: 'array (1, 2, 3, ect. )'
Array values is a two-dimentional array like : 'array ("1" => "VALUE", "2" =>
"VALUE2", ect.)'

Example:

****---------------------------------------****

Table: /databases/numbers.dbf

0 1 2 <--- Field numbers !!!
------------------------------
|Number1 | Number2 | Number3 |
|--------|---------|---------|
0 | 2 | 5 | 8 |
1 | 4 | 2 | 5 |
2 | 2 | 6 | 2 |
3 | 1 | 5 | 9 |
------------------------------
^
|-- Row numbers !!!!

****---------------------------------------****

$table_numbers = new dbase;
$table_numbers->connect (/databases/numbers.dbf);

echo ("$table_numbers->fields_num()<BR>");
echo ("$table_numbers->num_rows()<BR>");

$fields = array (1, 2)
$values = array ("0" => "2");
$data = $table_numbers->select_fields_where_values ($fields, $values);

print $data;
$table_numbers->result_current ();
$table_numbers->close();

****---------------------------------------****
Result of code above:

3
4
Array <--- This is a two-dimensional array
-------------
| 5 | 8 | <---(This is an HTML-table)
-------------
| 6 | 2 |
-------------

****---------------------------------------****

*/


class dbase
{
var $database_identifier,
$number_of_records,
         $number_of_fields,
         $data,
         $dbase_file;


function connect ($dbase_filename)
{
$this->database_identifier = dbase_open($dbase_filename, 2);
         $this->number_of_records = dbase_numrecords($this-
>database_identifier);
         $this->number_of_fields = dbase_numfields($this-
>database_identifier);
         $this->dbase_file = $dbase_filename;
}



function close ()
{
dbase_close($this->database_identifier);
}



function num_rows ()
{
return $this->number_of_records;
}



function fields_num ()
{
return $this->number_of_fields;
}



function create_table ($file_name, $fields_array)
{
if (dbase_create($file_name, $fields_array))
         $this->connect($file_name);
}



function insert_values ($data)
{
dbase_add_record( $this->database_identifier, $data );
$this->close();
         $this->connect($this->dbase_file);
}



function delete_rows_where_values ($values)
{
$nothing_to_delete = 0;

$i = 1;
while ($this->number_of_records >= $i)
{
$row = dbase_get_record($this->database_identifier, "$i");
$with_if = "{dbase_delete_record(\$this->database_identifier, \$i);
\$nothing_to_delete = 1; }";

eval( 'if ( '. $this->return_expr2($values) .')'. $with_if );

$i++;
}

if ($nothing_to_delete)
dbase_pack($this->database_identifier);

$this->close();
         $this->connect($this->dbase_file);
}



function select_all ()
{
$i = 1;
while ($this->number_of_records >= $i)
{
$row_data = dbase_get_record($this->database_identifier, "$i");
$table_data[] = $row_data;
$i++;
}

if ( !is_array($table_data) )
$table_data = 0;

$this->data = $table_data;
                 
         return $this->data;
}



function select_fields ($fields)
{
$i = 1;
while ($this->number_of_records >= $i)
{
$row_data = dbase_get_record($this->database_identifier, "$i");
$table_data[] = $this->return_fields($fields, $row_data);
$i++;
}

if ( !is_array($table_data) )
$table_data = 0;

$this->data = $table_data;
        
         return $this->data;
}



function select_all_where_values ($values)
{
$with_expr = "\$table_data[] = \$row_data;";

$i = 1;
while ($this->number_of_records >= $i)
{
$row_data = dbase_get_record($this->database_identifier, "$i");
eval( 'if ( '. $this->return_expr($values) .')'. $with_expr );

$i++;
}

if ( !is_array($table_data) )
$table_data = 0;

$this->data = $table_data;
        
         return $this->data;
}



function select_fields_where_values ($fields, $values)
{
$with_expr = "\$table_data[] = \$this->return_fields(\$fields,
\$row_data);";

$i = 1;
while ($this->number_of_records >= $i)
{
$row_data = dbase_get_record($this->database_identifier, "$i");
eval( 'if ( '. $this->return_expr($values) .')'. $with_expr );

$i++;
}

if ( !is_array($table_data) )
$table_data = 0;

$this->data = $table_data;
        
         return $this->data;
}



function result_current ()
{
if ($this->data)
         {
echo ("<TABLE BORDER=\"1\">\n");
reset($this->data);

do
{
$row_data = current($this->data);
                        echo ("<TR>");

         for ( reset($row_data); $row_element = current($row_data); next
($row_data) )
echo ("<TD>$row_element</TD>");

echo ("</TR>\n");
}
while ( next($this->data) );

echo ("</TABLE>\n");
         }
         else
         return 0;
}



function result_all ()
{
$table_data = $this->select_all();

if ( is_array($table_data) )
         {
echo ("<TABLE BORDER=\"1\">\n");
reset($table_data);

do
{
$row_data = current($table_data);
echo ("<TR>");
        
         for ( reset($row_data); $row_element = current($row_data); next
($row_data) )
echo ("<TD>$row_element</TD>");
                        
                        echo ("</TR>\n");
}
while ( next($table_data) );

echo ("</TABLE>\n");
}
         else
         return 0;
}



function return_fields ($fields, &$row_data)
{
while ( $fields_elements = each ($fields) )
{
$fields_array[] = $row_data[$fields_elements[value]];
}

return $fields_array;
}



function return_expr ($values)
{
while ( $bar = each( $values ) )
{
$expr .= "ereg(\"$bar[value]\", \$row_data[$bar[key]]) && ";
}

$expr = substr($expr, 0, strrpos( $expr, "&" ) - 2);

return $expr;
}



function return_expr2($values)
{
while ( $bar = each( $values ) )
{
$back .= "ereg(\"$bar[value]\", \$row[$bar[key]]) && ";
}

$back = substr($back, 0, strrpos( $text, "&&" ) - 3);

return $back;
}
} // end class dbase

?>



ADODB Database Wrapper Abstraction Library for PHP: MySQL, MSSQL, Oracle, Interbase,ODBC, Microsoft Access and FoxPro.
Categories : PHP Classes, Databases, PHP, General SQL, ODBC
explode() and SQL blobs
Categories : General SQL, Strings, PHP, Databases
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
The Ajax Tree view class fetches data from a db for the requested parent category id. The data is then stored in an array and converted into JSON (Javascript Object Notation) format. This format is then used by JavaScript for populating tree view.
Categories : PHP, PHP Classes, Java Script, AJAX, Databases
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
Is there some possibility to link a database to an htaccess file, so that instead of having a passwd file you would have a database with DES-crypted password and username fields?
Categories : Authentication, PHP, General SQL, Databases
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Simple class to create insert and update statements. Independent of the access to the database. Makes handling complex inserts easier - especially when the table structure is liable to change.
Categories : Databases, PHP Classes, PHP
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides