| Recommend this page to a friend! |
| MySQL Access | > | All threads | > | mysqlAccess.php | > | (Un) Subscribe thread alerts |
| |||||||||||||||
several bugs in added mysqlAccess.php
first one: line 51, if( !$this->isOpen ) if( !this->Open() ) return false; second one: when not using existing db-user/db-pass error-handling results in 404 third one (after this one i stopped trying): error while testing demo, Can't open database
Heres bugs free version. By the way. What's the point of posting unchecked class... stupid...
<?php include( 'settings.php' ); class MysqlConnection { var $rawConnection; var $error; var $errorNumber; var $isOpen; var $isInTransaction; function MysqlConnection() { $this->rawConnection = null; $this->error = ''; $this->errorNumber = null; $this->isOpen = false; $this->isInTransaction = false; } function Open( $forceNewConnection = false ) { $this->rawConnection = mysql_connect( DB_HOST, DB_USER, DB_PASS, $forceNewConnection ); if( !$this->rawConnection ) { header( 'Location: error?msg=' . urlencode( 'Can\'t connect to database' ) ); return false; } if( !@mysql_select_db( DB_NAME, $this->rawConnection ) ) { $this->error = @mysql_error( $this->rawConnection ); $this->errorNumber = @mysql_errno( $this->rawConnection ); header( 'Location: error?msg=' . $this->error ); return false; } $this->isOpen = true; return true; } function Close() { if( true != @mysql_close( $this->rawConnection ) ) { $this->error = @mysql_error( $this->rawConnection ); $this->errorNumber = @mysql_errno( $this->rawConnection ); header( 'Location: error?msg=' . $this->error ); return false; } $this->isOpen = false; return true; } function startTransaction() { if( $this->isInTransaction ) return false; if( !$this->isOpen ) if( !$this->Open() ) return false; if( true != mysql_query( 'BEGIN', $this->rawConnection ) ) { $this->error = @mysql_error( $this->rawConnection ); $this->errorNumber = @mysql_errno( $this->rawConnection ); header( 'Location: error?msg=' . $this->error ); return false; } $this->isInTransaction = true; return true; } function commitTransaction() { if( !$this->isInTransaction ) return false; if( !@mysql_query( 'COMMIT', $this->rawConnection ) ) { $this->error = @mysql_error( $this->rawConnection ); $this->errorNumber = @mysql_errno( $this->rawConnection ); header( 'Location: error?msg=' . $this->error ); return false; } $this->isInTransaction = false; return true; } function rollbackTransaction() { if( !$this->isInTransaction ) return false; if( !@mysql_query( 'ROLLBACK', $this->rawConnection ) ) { $this->error = @mysql_error( $this->rawConnection ); $this->errorNumber = @mysql_errno( $this->rawConnection ); header( 'Location: error?msg=' . $this->error ); return false; } $this->isInTransaction = false; return true; } } class MysqlQuery { var $connection; var $result; var $currentRow; var $EOF; var $numberOfRows; var $error; var $errorNumber; var $lastId; function MysqlQuery() { $this->connection = new MysqlConnection; $this->result = null; $this->currentRow = null; $this->EOF = true; $this->numberOfRows = 0; $this->error = ''; $this->errorNumber = 0; $this->lastId = 0 ; if( !$this->connection->Open() ) $this->error = 'Can\'t open database'; } function Execute( $queryString ) { if( !$this->connection->isOpen ) if( true != $this->connection->Open() ) return false; $this->result = @mysql_query( $queryString, $this->connection->rawConnection ); if( true != $this->result && 0 != mysql_errno( $this->connection->rawConnection ) ) { $this->error = @mysql_error( $this->connection->rawConnection ); $this->errorNumber = @mysql_errno( $this->connection->rawConnection ); return false; } $idResult = @mysql_query( 'select CAST( last_insert_id() AS unsigned )', $this->connection->rawConnection ); $idRow = mysql_fetch_array( $idResult, MYSQL_NUM ); $this->lastId = $idRow[0]; if( 1 == $this->result ) $this->numberOfRows = 0; else $this->numberOfRows = mysql_num_rows( $this->result ); if( 0 == $this->numberOfRows ) $this->EOF = true; else $this->EOF = false; return true; } function FetchNextRow() { if( $row = mysql_fetch_array( $this->result, MYSQL_ASSOC ) ) { $this->currentRow =& $row; $this->EOF = false; } else $this->EOF = true; return $row; } } ?>
Marcin,
Thank you very much for your version !!! It worked immediatly !! Just started self with OO php and couldn't find the problem.
Hy,
I'm sory for the bugs ... I didn't know that they are in there. I use that same class without modifications on several pages as it is here in mysqlAccess.php, and I never got into that kind of problems/bugs that chris bos mention it.
I'll rewrite the code in this days and hope that new code will be bugfree.
|
info at phpclasses dot org.
