MySQL Database Php

****
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()
");
echo ("$table_numbers->num_rows()
");
$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 ("\n");
reset($this->data);
do
{
$row_data = current($this->data);
echo ("");
for ( reset($row_data); $row_element = current($row_data); next
($row_data) )
echo ("");
echo ("\n");
}
while ( next($this->data) );
echo ("
$row_element
\n");
}
else
return 0;
}
function result_all ()
{
$table_data = $this->select_all();
if ( is_array($table_data) )
{
echo ("\n");
reset($table_data);
do
{
$row_data = current($table_data);
echo ("");
for ( reset($row_data); $row_element = current($row_data); next
($row_data) )
echo ("");
echo ("\n");
}
while ( next($table_data) );
echo ("
$row_element
\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
?>