SitePoint Sponsor |
|
User Tag List
Results 51 to 75 of 87
-
Nov 1, 2003, 03:16 #51
Originally Posted by shad0w
-
Nov 3, 2003, 03:03 #52
- Join Date
- Oct 2003
- Location
- Ukraine
- Posts
- 19
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Another code ...
Nice thread ... so, community, what will you say about this?
PHP Code:
<?php
/*
+--------------------------------------------------------------------------
| FlightDesign Internet Development Department
| =========================================
| by Cyrill Polikarpov
| (c) 2002-2003 FlightDesign
| [email]cyrill@f-design.com.ua[/email]
| =========================================
| Visit: [url]http://f-design.com.ua[/url]
| Email: [email]cyrill@f-design.com.ua[/email]
| Email: [email]clio@nix.zp.ua[/email]
| ICQ #: 99885395
+---------------------------------------------------------------------------
|
| > $$MYSQ.PHP
|
| > Version: 1.0.5
+--------------------------------------------------------------------------
*/
class mysql {
var $obj = array ( "sql_database" => "" ,
"sql_user" => "root" ,
"sql_pass" => "" ,
"sql_host" => "localhost",
"sql_port" => "" ,
"persistent" => "0" ,
"sql_tbl_prefix" => "fd_" ,
"cached_queries" => array() ,
);
var $query_id = "";
var $connection_id = "";
var $query_count = 0;
var $record_row = array();
var $return_die = 0;
var $error = "";
/*========================================================================*/
// Connect to the database
/*========================================================================*/
function connect() {
if ($this->obj['persistent'])
{
$this->connection_id = mysql_pconnect( $this->obj['sql_host'] ,
$this->obj['sql_user'] ,
$this->obj['sql_pass']
);
}
else
{
$this->connection_id = mysql_connect(
$this->obj['sql_host'] ,
$this->obj['sql_user'] ,
$this->obj['sql_pass']
);
}
if ( !mysql_select_db($this->obj['sql_database'], $this->connection_id) )
{
echo ("ERROR: Cannot find database ".$this->obj['sql_database']);
}
}
/*========================================================================*/
// Process a query
/*========================================================================*/
function query($the_query, $bypass=0) {
//--------------------------------------
// Change the table prefix if needed
//--------------------------------------
if ($bypass != 1)
{
if ($this->obj['sql_tbl_prefix'] != "fd_")
{
$the_query = preg_replace("/fd_(\S+?)([\s\.,]|$)/", $this->obj['sql_tbl_prefix']."\\1\\2", $the_query);
}
}
$this->query_id = mysql_query($the_query, $this->connection_id);
if (! $this->query_id )
{
$this->fatal_error("MySQL query error: $the_query");
}
$this->query_count++;
$this->obj['cached_queries'][] = $the_query;
return $this->query_id;
}
/*========================================================================*/
// Fetch a row based on the last query
/*========================================================================*/
function fetch_row($query_id = "") {
if ($query_id == "")
{
$query_id = $this->query_id;
}
$this->record_row = mysql_fetch_array($query_id, MYSQL_ASSOC);
return $this->record_row;
}
/*========================================================================*/
// Fetch the number of rows affected by the last query
/*========================================================================*/
function get_affected_rows() {
return mysql_affected_rows($this->connection_id);
}
/*========================================================================*/
// Fetch the number of rows in a result set
/*========================================================================*/
function get_num_rows() {
return mysql_num_rows($this->query_id);
}
/*========================================================================*/
// Return the amount of queries used
/*========================================================================*/
function get_query_cnt() {
return $this->query_count;
}
/*========================================================================*/
// Free the result set from mySQLs memory
/*========================================================================*/
function free_result($query_id="") {
if ($query_id == "") {
$query_id = $this->query_id;
}
@mysql_free_result($query_id);
}
/*========================================================================*/
// Shut down the database
/*========================================================================*/
function close_db() {
return mysql_close($this->connection_id);
}
/*========================================================================*/
// Basic error handler
/*========================================================================*/
function fatal_error($the_error) {
global $INFO;
// Are we simply returning the error?
if ($this->return_die == 1)
{
$this->error = mysql_error();
return TRUE;
}
$the_error .= "\n\nmySQL error: ".mysql_error()."\n";
$the_error .= "mySQL error code: ".mysql_errno()."\n";
$the_error .= "Date: ".date("l dS of F Y h:i:s A");
$out = "<html><head><title>Database Error</title>
<style>P,BODY{ font-family:arial,sans-serif; font-size:11px; }</style></head><body>
<br><br><blockquote><b>There appears to be an error with the database.</b><br>
You can try to refresh the page by clicking <a href=\"javascript:window.location=window.location;\">here</a>, if this
does not fix the error, you can contact the site administrator by clicking <a href='mailto:{$INFO['EMAIL_IN']}?subject=SQL+Error'>here</a>
<br><br><b>Error Returned</b><br>
<form name='mysql'><textarea rows=\"15\" cols=\"60\">".htmlspecialchars($the_error)."</textarea></form><br>We apologise for any inconvenience</blockquote></body></html>";
echo($out);
die("");
}
/*========================================================================*/
// Create an array from a multidimensional array returning formatted
// strings ready to use in an INSERT query, saves having to manually format
// the (INSERT INTO table) ('field', 'field', 'field') VALUES ('val', 'val')
/*========================================================================*/
function compile_db_insert_string($data) {
$field_names = "";
$field_values = "";
foreach ($data as $k => $v) {
$v = preg_replace( "/'/", "\\'", $v );
//$v = preg_replace( "/#/", "\\#", $v );
$field_names .= "$k,";
$field_values .= "'$v',";
}
$field_names = preg_replace( "/,$/" , "" , $field_names );
$field_values = preg_replace( "/,$/" , "" , $field_values );
return array( 'FIELD_NAMES' => $field_names,
'FIELD_VALUES' => $field_values,
);
}
/*========================================================================*/
// Create an array from a multidimensional array returning a formatted
// string ready to use in an UPDATE query, saves having to manually format
// the FIELD='val', FIELD='val', FIELD='val'
/*========================================================================*/
function compile_db_update_string($data) {
$return_string = "";
foreach ($data as $k => $v) {
$v = preg_replace( "/'/", "\\'", $v );
$return_string .= $k . "='".$v."',";
}
$return_string = preg_replace( "/,$/" , "" , $return_string );
return $return_string;
}
} // end class
?>
-
Nov 3, 2003, 06:20 #53
- Join Date
- Oct 2002
- Location
- Edinburgh, UK
- Posts
- 1,743
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
And what about separating connection class and result set class, like this:
PHP Code:<?
class Database
{
function Database ( $Host, $User, $Password, $Database )
{
}
function Query ( $Sql )
{
}
function Close ()
{
}
}
class Query
{
var $_Sql;
var $_Res;
var $_CurrentRow = 0;
var $_Error;
var $_IsSuccess = true;
function Query ( $Database, $Sql )
{
}
function _handleError ()
{
$this->_IsSuccess = false;
$this->_Error = mysql_error ();
print ( $this->getError () );
}
function getError ()
{
return $this->_Error . "<br />" . $this->_Sql . "<br />";
}
function getNumRows ()
{
}
function setRow ()
{
}
function getRow ()
{
}
}
//----------------------------------------------------------------------------------------------------------------
class MySql extends Database
{
function MySql ( $Host, $User, $Password, $Database )
{
mysql_connect ( $Host, $User, $Password );
mysql_select_db ( $Database );
}
function Query ( $Sql )
{
return new MySqlQuery ( $this, $Sql );
}
}
class MySqlQuery extends Query
{
function MySqlQuery ( $Database, $Sql )
{
$this->_Sql = $Sql;
$this->_Res = mysql_query ( $Sql ) or $this->_handleError ();
}
function getNumRows ()
{
return mysql_num_rows ( $this->_Res );
}
function setRow ( $Row )
{
return mysql_data_seek ( $this->_Res, $Row );
}
function getRow ()
{
if ( $this->_CurrentRow == $this->getNumRows () ) {
return false;
}
$row = mysql_fetch_array ( $this->_Res );
$this->_CurrentRow++;
return ( $row );
}
function Close ()
{
mysql_close ();
}
}
?>
-
Nov 3, 2003, 06:50 #54
Originally Posted by Clio
-
Nov 5, 2003, 08:45 #55
- Join Date
- Oct 2003
- Location
- Ukraine
- Posts
- 19
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I say that if you're going to steal the code from Invisionboard and pass it off as your own then you should do so in a slightly more subtle manner.(((( Sorry, i parsed another file-header. Really, this is InvisionBoard class for MySQL database, but i have my own! database classes for PgSQL, MSSQL and mSQL for IBforums, which wasn't included in original IBforums package, 'cause i don't have MySQL on my hosting-server. Sorry for codesample - i had no intention to arrogate to myself somebody else's code, it's just an accidental mistake
-
Nov 5, 2003, 09:29 #56
- Join Date
- Nov 2003
- Location
- The Netherlands
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ADODB evolution.
I've got a question about ADODB. Since codezilla seems to like it. Maybe he or someone else can answer it.
Browsing some php projects installed on my system, which all use ADODB, I saw the following (to me, shocking) facts.
The oldest version of adodb on my system is 3.31 which came with phpPgAdmin 3.0rc1
Lines of code for adodb.inc.php: 625
The second oldest version on my system is 3.60 which comes with the most recent postnuke version 0.726
Lines of code for adodb.inc.php: 3354
The most recent version on my system is 4.01 which I downloaded standalone.
Lines of code for adodb.inc.php: 3629
These lines don't include the lines of code for the adodb database drivers or other files which it might include.
My questions are:
- What are the reasons for this dramatic increase of code?
- Has anyone noticed significant increase of overhead / execution time over this evolution?
- if yes, does it pay (solved bugs/or usefull features) to use newer versions over older ones?
-
Nov 5, 2003, 10:13 #57
ADODb contains a very large amount of code designed to emulate various DB-specific functions in order to maintain cross-db compatibility. Which seems a bit silly to me as I don't think anyone in their right mind would rely on emulation of DB features rather than just coding for the DB should they ever migrate from one DB type to another.
-
Nov 5, 2003, 10:29 #58
- Join Date
- Nov 2002
- Location
- upstairs
- Posts
- 110
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Phil.Roberts, I used to think that way until I had a project that needed to work on Oracle. The problem was that the machine I was developing on did not have Oracle, and since it's extremely expensive my only option was to develop on one DB (MySQL) and deploy on another (Oracle). Using a DB abstraction layer made the deployment process very easy.
-
Nov 5, 2003, 11:45 #59
Personally if I were even in that situation I'd prefer to abstrate out the database calls altogether using the DAO pattern rather than mess about with re-writing proprietary SQL on the fly....
-
Nov 5, 2003, 13:54 #60
- Join Date
- Nov 2002
- Location
- upstairs
- Posts
- 110
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It's not about re-writing proprietary SQL, it's about not using database-dependent function calls. Essentially, it's like using DBX instead of having a set of MySQL code and a separate set of Oracle code.
-
Nov 5, 2003, 13:57 #61
- Join Date
- Nov 2003
- Location
- The Netherlands
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This discussion about ADODB being usefull or not was not really my intention.
I know there are people who think it's bloated. Although other people prefer it over PEAR::DB or phplib.
The point to my questions is:
What do 3000 lines in only a half year contribute to an already fully functional library of approximately 600 lines of code?
Of course this isn't really true, because I'm only talking about the main file. The only reason I can think of is that a lot of code from the other files was moved to the main file.
-
Nov 5, 2003, 14:00 #62
- Join Date
- Nov 2002
- Location
- upstairs
- Posts
- 110
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I really don't know much about the specifics of ADOdb. I just know that it works well for my needs. Sorry, I can't help you any more than that.
-
Nov 5, 2003, 20:50 #63
- Join Date
- Jun 2002
- Location
- Melbourne
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Between the versions of ADODB mentioned the RecordSet class and Connection classes were broken out into seperate files and then later merged back in again into the main ADODB file.
I think John Lim (ADODB author) determined that having separate files decreased performance.
The code size of the core library remained much the same. New optional features (like the awesome query profiling) have been added more recently as well, but they only appear in the main library file as function stubs for the most part, in order load the rest of the code.
-
Dec 4, 2003, 20:51 #64
Here is my MySQL class, i just finished it yesterday:
PHP Code:// Class: MySQL
class MySQL {
/* Initialize/Define Class Variables */
var $dbh;
var $dbs;
var $dba;
var $query;
var $result;
var $numrows;
var $row = array();
// Function: MySQL()
function MySQL() {
$this->dbh = mysql_connect(MYSQL_SERVER,MYSQL_UID,MYSQL_PWD) or die(mysql_error());
}
// Function: Select_DB()
function Select_DB() {
if($this->dba != NULL) {
$this->dbs = mysql_select_db($this->dba,$this->dbh) or die(mysql_error());
} else {
$this->dbs = mysql_select_db(MYSQL_DBA,$this->dbh) or die(mysql_error());
}
}
// Function: Disconnect()
function Disconnect() {
mysql_close($this->dbh);
}
// Function: Flush()
function Flush() {
mysql_free_result($this->result);
}
// Function: NumRows()
function NumRows() {
return $this->numrows = mysql_num_rows($this->result);
}
// Function: Query()
function Query() {
$this->result = mysql_query($this->query) or die(mysql_error());
}
// Function: FetchArray()
function FetchArray() {
return mysql_fetch_array($this->result);
}
// Function: FetchRow()
function FetchRow() {
return mysql_fetch_row($this->result);
}
}
-
Apr 12, 2004, 09:58 #65
i would have never thought of doing:
PHP Code:return MysqlStatment($sql,$this);
PHP Code:$this->connection->getConnectionId();
-
Apr 13, 2004, 17:44 #66
Originally Posted by shad0w
-
Jun 13, 2004, 01:15 #67
- Join Date
- Jun 2004
- Location
- Vardø, Norway
- Posts
- 68
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:// Previously MysqlConnectClass()
class MysqlConnection
{
(...)
function MysqlConnection($host, $database, $username, $password)
{
$this->host = $host;
$this->database = $database;
$this->username = $username;
$this->password = $password;
$this->open();
}
(...)
}
PHP Code:class Sillysoft_MysqlConnection extends MysqlConnection
{
var $dblocation = '*****';
var $dbname = '****';
var $dbpass = '**';
var $dbuser = '***';
function Sillysoft_MysqlConnection()
{
$this->MysqlConnection($dblocation, $dbname, $dbuser, $dbpass);
}
}
Secondly, I wonder what the & infront of functions is - is it simply that returns are passed by reference?
Great post btw, I'm currently writning my first large OOP application in PHP. Those tips on OOP thinking here was also very good - it's important to think of that when you're stuck in the middle of the coding having a few problems. I've been there a few times, making one big class with many tasks. Bad OOP, but it does the job(...not as efficent as you may want, I believe).
Concerning performance -- I've heard that OOP is slower than non-OOP in PHP; what makes it slower, and how to reduce this performance loss?
Regards,
Haakon
-
Jun 13, 2004, 01:34 #68
- Join Date
- Jun 2004
- Location
- Vardø, Norway
- Posts
- 68
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I almost forgot.. How to integrate error handling in these classes (from page one)? Do you implement it as one external class for errors, or do you incorportate them into the exsisting classes?
Regards,
Haakon
-
Jun 13, 2004, 04:58 #69
- Join Date
- May 2004
- Location
- Germany
- Posts
- 550
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:class MysqlError
{
var $statement;
var $file;
var $line;
var $error;
function MysqlError($statement, $file, $line) {
$this->statement = & $statement;
$this->file = $file;
$this->line = $line;
$this->CreateError();
$this->DisplayError();
}
function CreateError()
{
$this->error = 'MysqlError: '. mysql_error($this->statement->connection->getConnectionId()).'<br />';
$this->error .= 'Statement: '.$this->statement->getPreparedSql().'<br />';
$this->error .= 'File: '.$this->file.'<br />';
$this->error .= 'Line: '.$this->line.'<br />';
}
function DisplayError()
{
echo $this->error;
exit;
}
}
I used Codezilla's Connection, Statement and ResultSet Classes, only change was in MysqlStatement:
PHP Code:function &execute($file = '', $line = '')
{
if($resultId = mysql_query($this->getPreparedSql(), $this->connection->getConnectionId())) {
return new MysqlResultSet($resultId);
}else{
return new MysqlError($this, $file, $line);
};
}
Edit:
One idea i just got:
What about letting DisplayError decide wether (how is it spelled?) to exit/die the script, or continue with only a Warning/Notice?
I always let the script die when an sql-error occurs
Another Point:
My Error class only handles mysql_query errors, but if an error during the connection occurs (bad password, user or database) i don't want to have the mysql_error text but one like: "Could not connect to Server" or "Could not select Database" How do i solve this the best way?
-
Jun 13, 2004, 06:23 #70
- Join Date
- May 2004
- Location
- Germany
- Posts
- 550
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've kept working on it
PHP Code:class MysqlError
{
var $statement;
var $file;
var $line;
var $error;
var $errortype;
function MysqlError($statement, $file, $line, $type = 'statement') {
$this->statement = & $statement;
$this->file = $file;
$this->line = $line;
$this->errortype = $type;
$this->CreateError();
$this->DisplayError();
}
function CreateError()
{
switch ($this->errortype) {
case 'statement':
$this->error = 'MysqlError: '. mysql_error($this->statement->connection->getConnectionId()).'<br />';
$this->error .= 'Statement: '.$this->statement->getPreparedSql().'<br />';
$this->error .= 'File: '.$this->file.'<br />';
$this->error .= 'Line: '.$this->line.'<br />';
break;
case 'connect':
$this->error = 'An error occured during connection to the server!<br />';
break;
case 'select':
$this->error = 'An error occured selecting the database!<br />';
break;
}
}
function DisplayError()
{
echo $this->error;
exit;
}
}
PHP Code:function open()
{
if($this->connectionId = @mysql_connect($this->host, $this->username, $this->password)) {
if(!@mysql_select_db($this->database, $this->connectionId)) {
return new MysqlError('', '', '', 'select');
};
}else{
return new MysqlError('', '', '', 'connect');
}
}
But i want $type to be the last param, because you usually connect once to the server and query it more than once.
Any suggestions how to solve this?
-
Jun 13, 2004, 07:12 #71
- Join Date
- Dec 2003
- Location
- oz
- Posts
- 819
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by codezilla
Originally Posted by Phil.Roberts
Originally Posted by Phil.Roberts
-
Jun 13, 2004, 09:34 #72
Originally Posted by lazy_yogi
-
Jun 13, 2004, 10:17 #73
- Join Date
- Jun 2004
- Location
- Atlanta, GA
- Posts
- 21
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by lazy_yogi
-
Jun 13, 2004, 17:00 #74
- Join Date
- Jun 2004
- Location
- Vardø, Norway
- Posts
- 68
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Like.... prepared statements? Now, what good do they do?
http://dev.mysql.com/doc/mysql/en/C_...tatements.html ... however, I dont think I'm quite getting the points. Its quicker when you want to do many of the same query? Now when would you do that?
I got really confused by this.
D'oh.
Regards,
Haakon
-
Jun 13, 2004, 19:31 #75
- Join Date
- Jun 2003
- Location
- Chicago
- Posts
- 73
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by codezilla
Hi,
First, that was an amazing post. I'm changing my DB class to reflect some of the things you said... although, I have one question.
Wouldn't using sprintf in getPreparedStatement be more efficient then the explode/loop method? I think it achieves basically the same... or is it wrong for this purpose? Please help me to udnerstand which method would be better.
Thanks,
Phil
Bookmarks