Re: [PHP] MySQL class. Thoughts?

2009-01-22 Thread clive

Chris wrote:


That won't tell you where a query comes from ;) Add a debug_backtrace 
into the class to also pinpoint where the query was called from. 
Complicated queries built on variables (or even just long queries 
built over multiple lines) will be hard to find just by looking at the 
mysql query log.


I agree with chris and I do the same thing, also with a class or a 
wrapper function/s that handle your queries you can also decided on what 
databases the query should run, say you have Master-Slave  replication 
setup with mysql, you could run selects on the slaves and updates/delete 
in the master without the class deciding which one it should run on


Clive


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] MySQL class. Thoughts?

2009-01-21 Thread Jay Moore
This is a MySQL class I use and I wanted to get everyone's thoughts on 
how/if I can improve it.  This is for MySQL only.  I don't need to make 
it compatible with other databases.  I'm curious what you all think.


Thanks,
Jay

Class:
--
?php

// Standard MySQL class
class do_mysql
{   
// Constructor
function __construct()
{
$this-do_mysql();
}

// Destructor
function __destruct()
{
//$this-close();
}

function do_mysql()
{
$this-login = '';
$this-pass = '';

		$this-link = @mysql_connect('localhost', $this-login, $this-pass) 
or die('Could not connect to the database.');

} // End do_mysql

// Functions
function close()
{
if ($this-link)
{
mysql_close($this-link);
unset($this-link);
}
} // End close

function fetch_array()
{
return mysql_fetch_array($this-result);
} // End fetch_array

function last_id()
{
return mysql_insert_id($this-link);
} // End last_id

function num_rows()
{
return mysql_num_rows($this-result);
} // End num_rows

function process($database = '')
{
if (is_null($this-query))
{
die('Error:  Query string empty.  Cannot proceed.');
}

		$this-db = @mysql_select_db($database, $this-link) or die(Database 
Error:  Couldn't select $database br / . mysql_error());
		$this-result = @mysql_query($this-query, $this-link) or 
die('Database Error:  Couldn\'t query. br /' . mysql_error() . br 
/br / $this-query);

} // End process

function sanitize($ref)
{
$ref = mysql_real_escape_string($ref);
} // End sanitize

} // End do_mysql

?


Sample usage:
$value = 'value';
$sql = new do_mysql();
$sql-sanitize($value);
$sql-query = SELECT * FROM `wherever` WHERE `field` = '$value';
$sql-process('dbname');
$sql-close();

if ($sql-num_rows())
{
while ($row = $sql-fetch_array())
{
do stuff;
}
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Edmund Hertle
2009/1/21 Jay Moore jaymo...@accu-com.com

 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make it
 compatible with other databases.  I'm curious what you all think.

 Thanks,
 Jay


Hey,
1. You know the mysqli-Class?
2. If yes, than I don't get it in which way this will improve mysql handling

 -eddy


Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Kyle Terry
On Wed, Jan 21, 2009 at 9:45 AM, Edmund Hertle
edmund.her...@student.kit.edu wrote:
 2009/1/21 Jay Moore jaymo...@accu-com.com

 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make it
 compatible with other databases.  I'm curious what you all think.

 Thanks,
 Jay


 Hey,
 1. You know the mysqli-Class?
 2. If yes, than I don't get it in which way this will improve mysql handling

  -eddy


MySQLi actually stands for MySQL Improved. Problem solved.

-- 
Kyle Terry | www.kyleterry.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Eric Butera
On Wed, Jan 21, 2009 at 12:45 PM, Edmund Hertle
edmund.her...@student.kit.edu wrote:
 2009/1/21 Jay Moore jaymo...@accu-com.com

 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make it
 compatible with other databases.  I'm curious what you all think.

 Thanks,
 Jay


 Hey,
 1. You know the mysqli-Class?
 2. If yes, than I don't get it in which way this will improve mysql handling

  -eddy


Yea if you're only targeting 1 db, then why not use that class?  At
least then there's the php manual to figure out what something does.

mysql_real_escape_string should use $this-link to properly escape
based on charset, not server default.  I'd also call it escape.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Paul M Foster
On Wed, Jan 21, 2009 at 11:37:07AM -0600, Jay Moore wrote:

 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make
 it compatible with other databases.  I'm curious what you all think.

 Thanks,
 Jay

 Class:
 --
 ?php

 // Standard MySQL class
 class do_mysql
 {
   // Constructor
   function __construct()
   {
   $this-do_mysql();
   }

   // Destructor
   function __destruct()
   {
   //$this-close();
   }

   function do_mysql()
   {
   $this-login = '';
   $this-pass = '';

   $this-link = @mysql_connect('localhost', $this-login,
   $this-pass)
 or die('Could not connect to the database.');
   } // End do_mysql

   // Functions
   function close()
   {
   if ($this-link)
   {
   mysql_close($this-link);
   unset($this-link);
   }
   } // End close

   function fetch_array()
   {
   return mysql_fetch_array($this-result);
   } // End fetch_array

   function last_id()
   {
   return mysql_insert_id($this-link);
   } // End last_id

   function num_rows()
   {
   return mysql_num_rows($this-result);
   } // End num_rows

   function process($database = '')
   {
   if (is_null($this-query))
   {
   die('Error:  Query string empty.  Cannot proceed.');
   }

   $this-db = @mysql_select_db($database, $this-link)
   or die(Database
 Error:Couldn't select $database br / . mysql_error());
   $this-result = @mysql_query($this-query, $this-link) or
 die('Database Error:  Couldn\'t query. br /' . mysql_error() . br
 /br / $this-query);
   } // End process

   function sanitize($ref)
   {
   $ref = mysql_real_escape_string($ref);
   } // End sanitize

 } // End do_mysql

 ?


 Sample usage:
 $value = 'value';
 $sql = new do_mysql();
 $sql-sanitize($value);
 $sql-query = SELECT * FROM `wherever` WHERE `field` = '$value';
 $sql-process('dbname');
 $sql-close();

 if ($sql-num_rows())
 {
   while ($row = $sql-fetch_array())
   {
   do stuff;
   }
 }


A couple of thoughts. First precede all your mysql_* calls with the at
sign (@) to shut up the routines if they generate text. I had this
problem, and that was the answer.

Second, store your connection resource as a class variable, so you can
pass it around to the various routines. Actually, you're already doing
this, but I prefer to do so explicitly, as:

var $link;

at the top of the class.

I have a similar class for PostgreSQL. I also have routines like
update, which allow you to pass a table name and an associative array
of field values. Same thing for an insert routine.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Richard Heyes
 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make it
 compatible with other databases.  I'm curious what you all think.

I have a similar thing I use, which uses the same (or at least very
similar) API to the PEAR::DB abstraction layer.

http://www.phpguru.org/downloads/DB/

-- 
Richard Heyes

HTML5 Graphing for Firefox, Chrome, Opera and Safari:
http://www.rgraph.org (Updated January 17th)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Jochem Maas
Paul M Foster schreef:
 On Wed, Jan 21, 2009 at 11:37:07AM -0600, Jay Moore wrote:
 
 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make
 it compatible with other databases.  I'm curious what you all think.


I'd try to move to using the mysqli functions or class, which
you can still wrap in a custom object (I do this because
I like to minimize the interface to the bare, bare minimum ...
professional laziness you might say).

does you class need to be php4 compatible? I'd hope
not but you may still have to support php4 ... even then
I'd doubt you'd be using php4 for new project so it
might be worth making a new php5 only class.

...

 Thanks,
 Jay

 Class:
 --
 ?php

 // Standard MySQL class
 class do_mysql
 {
  // Constructor
  function __construct()
  {
  $this-do_mysql();
  }

  // Destructor
  function __destruct()
  {
  //$this-close();
  }

  function do_mysql()
  {
  $this-login = '';
  $this-pass = '';

  $this-link = @mysql_connect('localhost', $this-login,
  $this-pass)
 or die('Could not connect to the database.');
  } // End do_mysql

  // Functions
  function close()
  {
  if ($this-link)
  {
  mysql_close($this-link);
  unset($this-link);
  }
  } // End close

  function fetch_array()
  {
  return mysql_fetch_array($this-result);
  } // End fetch_array

  function last_id()
  {
  return mysql_insert_id($this-link);
  } // End last_id

  function num_rows()
  {
  return mysql_num_rows($this-result);
  } // End num_rows

  function process($database = '')
  {
  if (is_null($this-query))
  {
  die('Error:  Query string empty.  Cannot proceed.');
  }

  $this-db = @mysql_select_db($database, $this-link)
  or die(Database
 Error:   Couldn't select $database br / . mysql_error());
  $this-result = @mysql_query($this-query, $this-link) or
 die('Database Error:  Couldn\'t query. br /' . mysql_error() . br
 /br / $this-query);
  } // End process

  function sanitize($ref)
  {
  $ref = mysql_real_escape_string($ref);
  } // End sanitize

 } // End do_mysql

 ?


 Sample usage:
 $value = 'value';
 $sql = new do_mysql();
 $sql-sanitize($value);
 $sql-query = SELECT * FROM `wherever` WHERE `field` = '$value';
 $sql-process('dbname');
 $sql-close();

 if ($sql-num_rows())
 {
  while ($row = $sql-fetch_array())
  {
  do stuff;
  }
 }

 

here's another (php5) version of your class,
see what you think:

?php

class do_mysql
{
private $link;
private $result;
private $db;

// Constructor
function __construct($login, $pass, $db, $server = 'localhost')
{
$this-link = mysql_connect($server, $login, $pass);

if (!$this-link)
throw new Exception('Could not connect to the database.');

$this-setDB($db);
}

// Destructor
function __destruct()
{
$this-close();
}

// Functions
function close()
{
if (!$this-link)
return;

mysql_close($this-link);
unset($this-link);

}

function fetch_array()
{
return mysql_fetch_array($this-result);
}

function last_id()
{
return mysql_insert_id($this-link);
}

function num_rows()
{
return mysql_num_rows($this-result);
}

function setDB($database)
{
if (!$database || !mysql_select_db($database, $this-link))
throw new Exception(Database Error:  Couldn't select $database 
-  . mysql_error());
}

function query($query)
{
if (empty($query))
throw new Exception('Error:  Query string empty.  Cannot 
proceed.');

$this-result = mysql_query($this-query, $this-link);

if (!$this-result) // beware that putting this msg on screen is a 
security hazard
throw new Exception('Database Error:  Couldn\'t query - ' . 
mysql_error() .  - $query);
}

function escape($ref)
{
return mysql_real_escape_string($ref, $this-link);
}

}

 A couple of thoughts. First precede all your mysql_* calls with the at
 sign (@) to shut up the routines if they generate text. I had this
 problem, and that was the answer.

yes that's very bad advice. using error suppression is bad for performance
and debugging, don't do it unless you really really have to (e.g. you
have some function that spits warnings even with display_errors set to Off)

display_errors should be set to Off in production, and errors/warnings shouldn't
be suppressed, they should be logged.

handle 

Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Philip Graham


On January 21, 2009 12:37:07 Jay Moore wrote:
 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make
 it compatible with other databases.  I'm curious what you all think.

I definetly think that using a DB class is a really good idea, but the 
interface to your object could use some improvement.


 Sample usage:
 $value = 'value';
 $sql = new do_mysql();
 $sql-sanitize($value);
 $sql-query = SELECT * FROM `wherever` WHERE `field` = '$value';
 $sql-process('dbname');
 $sql-close();

 if ($sql-num_rows())
 {
   while ($row = $sql-fetch_array())
   {
   do stuff;
   }
 }

One of the goals of encapsulating DB access should be to hide some of the 
tediousness of database access, however, using the interface for your object 
you're still writing just as many (if not more) lines of code as you would by 
simply using the mysql_* functions directly.

As an example of how you can put this into practice, here's the interface to 
the DB object that I use:

The object is implemented as a singleton.  When retrieved initially the 
constructor deals with setting up the connection using constant defined in the 
class for username, password, dbName and server address.  The instance itself 
wraps a PDO connection and provides the following interface:

// Query functions
query($query, $queryName = null, $taskName = null);
prepare($query, $queryName, $taskName = null);
execute($queryName, array $params = array(), $taskName = null);
static sanitize($string);

// Data retrieval functions
getResults($queryName, $taskName = null);
getColumn($queryName, $taskName = null, $columnIndex = 0);
getRow($queryName, $taskName = null, $rowIndex = 0);
getCell($queryName, $taskName = null, $rowIndex = 0, $columnIndex = 0);

// Storage control
reset($queryName, $taskName = null);
resetAll($taskName = null);

The interface is clean and lets me handle most common cases with readable 
code:

// Retrieve one piece of data from the database
try {
$db = DB::getInstance();
$db-prepare('SELECT COUNT(*) FROM users WHERE id=:userid', 
'getNumUsers');
$db-execute('getNumUsers', array(':userid' = $userId));
$numUsers = $db-getCell('getNumUsers');

// Retrieve a row from the database
if($numUsers == 1) {
$db-prepare('SELECT * FROM users WHERE id=:userid', 
'getUserInfo');
$db-execute('getUserInfo', array(':userid' = $userId));
$userInfo = $db-getRow('getUserInfo');

// Retrieve multiple rows from the data
$db-prepare('SELECT friend_id FROM user_friends WHERE 
user_id=:userid', 
'getUsersFriends');
$db-execute('getUsersFriends', array(':userid' = $userId));
foreach($db-getColumn('getUsersFriends') AS $friendId) {
$db-execute('getUserInfo', array(':userid'= 
$friendId));
$friendInfo = $db-getRow('getUserInfo');
// .
}
}
$db-reset('getNumUsers');
$db-reset('getUserInfo');
$db-reset('getUsersFiends');
} catch(DBException $exception) {
echo $exception;
}

Since the object wraps a PDO object, there's no need to sanitize, just 
parameterize anything that may be unsafe in a prepared statement.  The 
retrieval functions will all return the data in a form that's easy to access 
so you don't need to worry about getting an associative array when all you 
want is a single cell or worry about getting a table when all you want is a 
single row.  This helps to reduces clutter in your code.  Any PDOExceptions 
are wrapped with the DBException class that implements an __toString method 
that outputs a nice message to make error output clean and consistent.

The interface for you object does little more than replace the mysql_* 
function calls with calls to an equivalent function in your oject.

Re-writing your example from above:

$db = DB::getInstance();
$value = 'value';
$db-prepare('SELECT * FROM wherever WHERE field=:val, 'getData');
$db-execute('getData', array(':val' = $value));
foreach($db-getResults('getData') AS $row) {
// Do stuff
}

Resulting in a reduction of 5 lines of code.  I may not sound that impressive 
for a small example but over the number of times you generally access a 
database it will really add up.
 
-- 
Philip Graham
Lightbox Technologies
www.lightbox.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Jay Moore

Good ideas guys.  The input is much appreciated.

Jochem (and anyone else, I guess), as I am not 100% versed with 
Exceptions, the php5 version you suggested, are those Exceptions able to 
be handled outside the class?


Do I need my try block to be within the class block, or can I have the 
try block be in my normal code where I actually instantiate the class?


This:

class blah
{
try { stuff } catch (exception) { more stuff }
}

$i = new blah()

or can I do this:

class blah
{
do some stuff (no try/catch blocks here)
throw an exception
}

try
{

$i = new blah();
more stuff
}
catch (exception) { even more stuff }


Thanks,
Jay

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Paul M Foster
On Wed, Jan 21, 2009 at 09:10:54PM +0100, Jochem Maas wrote:

 Paul M Foster schreef:
  On Wed, Jan 21, 2009 at 11:37:07AM -0600, Jay Moore wrote:
 
  This is a MySQL class I use and I wanted to get everyone's thoughts on
  how/if I can improve it.  This is for MySQL only.  I don't need to make
  it compatible with other databases.  I'm curious what you all think.
 
 
 I'd try to move to using the mysqli functions or class, which
 you can still wrap in a custom object (I do this because
 I like to minimize the interface to the bare, bare minimum ...
 professional laziness you might say).
 
 does you class need to be php4 compatible? I'd hope
 not but you may still have to support php4 ... even then
 I'd doubt you'd be using php4 for new project so it
 might be worth making a new php5 only class.

I always write with PHP4 in mind. You never know. Besides, I can always
change the internal implementation, if it's a class.

snip

 
  A couple of thoughts. First precede all your mysql_* calls with the at
  sign (@) to shut up the routines if they generate text. I had this
  problem, and that was the answer.
 
 yes that's very bad advice. using error suppression is bad for performance
 and debugging, don't do it unless you really really have to (e.g. you
 have some function that spits warnings even with display_errors set to Off)
 
 display_errors should be set to Off in production, and errors/warnings
 shouldn't
 be suppressed, they should be logged.
 
 handle errors gracefully ('or die()' is not graceful)

I don't know about performance. But every call to the query() method of
my class tests to see the results of the query. If it failed, I call the
proper function from PostgreSQL to fetch the error, and store it in the
class. The query function returns false, if there's an error. So the
user can test the return and then call a function to echo the stored
error.


 
  Second, store your connection resource as a class variable, so you can
  pass it around to the various routines. Actually, you're already doing
  this, but I prefer to do so explicitly, as:
 
  var $link;
 
 that's very php4.

Yep.

 
  at the top of the class.
 
  I have a similar class for PostgreSQL. I also have routines like
  update, which allow you to pass a table name and an associative array
  of field values. Same thing for an insert routine.
 
 if the postgres extension is anything like the firebird extension then
 there may actually be a few calls which do require error suppression :-)
 

Probably. When I first started coding PHP/PostgreSQL, I was getting
stuff printed out from the pg_* routines on the webpages and screwing
things up. I was on the PostgreSQL list at the time, and sent an email
to the list about chatter from the pg_* routines. The solution was to
prefix them with @. Works great, but I *do* check for and capture error
text and such.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Paul M Foster
On Wed, Jan 21, 2009 at 02:30:00PM -0600, Jay Moore wrote:

 Good ideas guys.  The input is much appreciated.

 Jochem (and anyone else, I guess), as I am not 100% versed with
 Exceptions, the php5 version you suggested, are those Exceptions able to
 be handled outside the class?

 Do I need my try block to be within the class block, or can I have the
 try block be in my normal code where I actually instantiate the class?

 This:

 class blah
 {
   try { stuff } catch (exception) { more stuff }
 }

 $i = new blah()

 or can I do this:

 class blah
 {
   do some stuff (no try/catch blocks here)
   throw an exception
 }

 try
 {

   $i = new blah();
   more stuff
 }
 catch (exception) { even more stuff }


I know it's very OO-y to use exceptions, but I hate them. They're like
setjmp/longjmp calls in C, and they're a really headache to deal with.
If you don't use default or predone handlers, you have to put all kinds
of try/catch blocks around everything. They make for non-linear
execution, and I prefer my code to execute in a linear fashion.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Jochem Maas
Jay Moore schreef:
 Good ideas guys.  The input is much appreciated.
 
 Jochem (and anyone else, I guess), as I am not 100% versed with
 Exceptions, the php5 version you suggested, are those Exceptions able to
 be handled outside the class?
 
 Do I need my try block to be within the class block, or can I have the
 try block be in my normal code where I actually instantiate the class?
 
 This:
 
 class blah
 {
 try { stuff } catch (exception) { more stuff }
 }
 
 $i = new blah()

no, this does not work, 'snot even valid syntax.

 
 or can I do this:
 
 class blah
 {
 do some stuff (no try/catch blocks here)
 throw an exception
 }
 
 try
 {
 
 $i = new blah();
 more stuff
 }
 catch (exception) { even more stuff }

yes.

try {
$db = new do_mysql('root', 'pass', 'mydb');
} catch (Exception $e) {
// var_dump($e); // debug
// could not connect, deal with it.
}

// do some stuff

try {
$db-query($myQry);
} catch (Exception $e) {
// bad query, deal with
}

... etc, etc

 Thanks,
 Jay
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Jay Moore



I know it's very OO-y to use exceptions, but I hate them. They're like
setjmp/longjmp calls in C, and they're a really headache to deal with.
If you don't use default or predone handlers, you have to put all kinds
of try/catch blocks around everything. They make for non-linear
execution, and I prefer my code to execute in a linear fashion.

Paul



My thoughts exactly.  What do I gain by using a try/catch that I lose by 
using if/else or similar?


J

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Jochem Maas
Jay Moore schreef:
 
 I know it's very OO-y to use exceptions, but I hate them. They're like
 setjmp/longjmp calls in C, and they're a really headache to deal with.
 If you don't use default or predone handlers, you have to put all kinds
 of try/catch blocks around everything. They make for non-linear
 execution, and I prefer my code to execute in a linear fashion.

 Paul

 
 My thoughts exactly.  What do I gain by using a try/catch that I lose by
 using if/else or similar?

you use them not for control flow, but for deferring exceptional application
states, which you can then handle in a small number of places as opposed
to scattering the error handling of unlikely events in all sorts of
disparate places.

there is an art to using them, they compliment 'traditional' error handling,
and I agree they can hinder if used badly.

NB: you can have multiple catch blocks:

try {
// do lots of stuff
} catch (ConnectException $e) {

} catch (QueryException $e) {

} catch (DBSelectException $e) {

} catch (Exception $e) {
// catch all for stuff we don't handle specifically
}

 J
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Török Alpár
2009/1/21 Jay Moore jaymo...@accu-com.com


  I know it's very OO-y to use exceptions, but I hate them. They're like
 setjmp/longjmp calls in C, and they're a really headache to deal with.
 If you don't use default or predone handlers, you have to put all kinds
 of try/catch blocks around everything. They make for non-linear
 execution, and I prefer my code to execute in a linear fashion.

 this also means you use a single return in a function ?



 Paul


 My thoughts exactly.  What do I gain by using a try/catch that I lose by
 using if/else or similar?

the power to omit it, and then change your mind  lather, and handle them.
The only problem is that php itself (nor core, nor extensions) throw
exceptions, so you can't threat errors uniformly, but you cant have a custom
error handler, and throw exceptions from there, and you can also have an
exception handler. I have used this approach in several projects, and i
admit it takes some getting used to, but it's an easy, clean, nice way to
handle exceptions / errors / conditions.



 J

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Torok, Alpar Istvan


Re: Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread ceo

 there is an art to using them, they compliment 'traditional' error

 handling, and I agree they can hinder if used badly.



I don't think I've ever seen Exceptions used well...



Invariably, I end up having to write a wrapper function around every function 
implemented and catch all the Exceptions.



Otherwise, my code is littered with try/catch blocks for every little thing the 
other guy was too lazy to figure out how to handle gracefully.



ymmv



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Nathan Rixham

c...@l-i-e.com wrote:

there is an art to using them, they compliment 'traditional' error
handling, and I agree they can hinder if used badly.


I don't think I've ever seen Exceptions used well...

Invariably, I end up having to write a wrapper function around every function 
implemented and catch all the Exceptions.

Otherwise, my code is littered with try/catch blocks for every little thing the 
other guy was too lazy to figure out how to handle gracefully.

ymmv



i use them often, basically if a boolean false won't do its a case of 
throwing an exception.


let's say you have:

calls_fifty_methods($page_load_of_variables);

wrap that bit in a try catch and you get


try {

  calls_fifty_methods($page_load_of_variables);

} catch ( DatabaseException $e) {
  // handle that error
} catch ( FileNotFoundException $e) {
  // handle
} catch ( VerySpecificException) {
  // handle
} catch ( Exception $e ) {
  // didn't expect this, notify devs, error log it and do X Y Z
}

try firing back error codes or something from 50 methods down and you 
have a real can go wrong easily series of returning error codes and 
processing the same anyways; or you could take the echo some html 
approach from the function - which is wrong on so many levels or..


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Paul M Foster
On Wed, Jan 21, 2009 at 10:00:53PM +0100, Jochem Maas wrote:

 Jay Moore schreef:
 
  I know it's very OO-y to use exceptions, but I hate them. They're like
  setjmp/longjmp calls in C, and they're a really headache to deal with.
  If you don't use default or predone handlers, you have to put all kinds
  of try/catch blocks around everything. They make for non-linear
  execution, and I prefer my code to execute in a linear fashion.
 
  Paul
 
 
  My thoughts exactly.  What do I gain by using a try/catch that I lose by
  using if/else or similar?
 
 you use them not for control flow, but for deferring exceptional application
 states, which you can then handle in a small number of places as opposed
 to scattering the error handling of unlikely events in all sorts of
 disparate places.
 
 there is an art to using them, they compliment 'traditional' error handling,
 and I agree they can hinder if used badly.

I understand, but then it comes down to how you define an exceptional
application state. If I build a date class that throws exceptions, it's
very OO-y, but a waste of my programmer time. I could just as easily
build an error checker into the class and use that to check for errors.
Generally speaking, my next step is going to be to display the page
again and tell the user to retype the date properly anyway.

There are reasons to use setjmp and longjmp in C as well, but I only
ever used them once, and later ripped out that code.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Jochem Maas
Nathan Rixham schreef:
 c...@l-i-e.com wrote:
 there is an art to using them, they compliment 'traditional' error
 handling, and I agree they can hinder if used badly.

 I don't think I've ever seen Exceptions used well...

 Invariably, I end up having to write a wrapper function around every
 function implemented and catch all the Exceptions.

 Otherwise, my code is littered with try/catch blocks for every little
 thing the other guy was too lazy to figure out how to handle gracefully.

 ymmv

 
 i use them often, basically if a boolean false won't do its a case of
 throwing an exception.
 
 let's say you have:
 
 calls_fifty_methods($page_load_of_variables);
 
 wrap that bit in a try catch and you get
 
 
 try {
 
   calls_fifty_methods($page_load_of_variables);
 
 } catch ( DatabaseException $e) {
   // handle that error
 } catch ( FileNotFoundException $e) {
   // handle
 } catch ( VerySpecificException) {
   // handle
 } catch ( Exception $e ) {
   // didn't expect this, notify devs, error log it and do X Y Z
 }
 
 try firing back error codes or something from 50 methods down and you
 have a real can go wrong easily series of returning error codes and
 processing the same anyways; or you could take the echo some html
 approach from the function - which is wrong on so many levels or..
 

agreed, that basically the point, being able to defer the handling of
the problem to some code that knows what to do with it given the context,
if a DB object cannot connect to the DB how does it know what to do in
terms of handling the situation? ... in a webpage you may wish to
redirect, show an error msg or something else (which shouldn't be up
to the object in question and should therefore not be built into it),
in a CLI environment you'd want to do something else maybe.

wrapper functions to handle exceptions as Richard offered is actually
quite neat assuming that the wrapper functions are designed for a
given context (and that they can then assume to be able to handle
the given exception in a way suitable to the given context)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Chris

Eric Butera wrote:

On Wed, Jan 21, 2009 at 12:45 PM, Edmund Hertle
edmund.her...@student.kit.edu wrote:

2009/1/21 Jay Moore jaymo...@accu-com.com


This is a MySQL class I use and I wanted to get everyone's thoughts on
how/if I can improve it.  This is for MySQL only.  I don't need to make it
compatible with other databases.  I'm curious what you all think.

Thanks,
Jay


Hey,
1. You know the mysqli-Class?
2. If yes, than I don't get it in which way this will improve mysql handling

 -eddy



Yea if you're only targeting 1 db, then why not use that class?  At
least then there's the php manual to figure out what something does.


Because then to add query logging for the whole app, you just need to 
put it in the class :)


(I've done that before to check what's being run and where from, comes 
in very handy).


--
Postgresql  php tutorials
http://www.designmagick.com/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread ceo

I prefer to deal with the issues locally, or have a documented behaviour with 
return values and error details available, much like most of PHP 
extensions/internals.



try/catch ends up with weird code organization, imho, especially when you can 
only really handle some exceptions.



For real fun, one API layer will catch all the lower exceptions, then wrap them 
in a generic exception, and you have to string parse $e-getMessage() to figure 
out what to *do*. [shudder]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Eric Butera
On Wed, Jan 21, 2009 at 5:53 PM, Chris dmag...@gmail.com wrote:
 Eric Butera wrote:

 On Wed, Jan 21, 2009 at 12:45 PM, Edmund Hertle
 edmund.her...@student.kit.edu wrote:

 2009/1/21 Jay Moore jaymo...@accu-com.com

 This is a MySQL class I use and I wanted to get everyone's thoughts on
 how/if I can improve it.  This is for MySQL only.  I don't need to make
 it
 compatible with other databases.  I'm curious what you all think.

 Thanks,
 Jay

 Hey,
 1. You know the mysqli-Class?
 2. If yes, than I don't get it in which way this will improve mysql
 handling

  -eddy


 Yea if you're only targeting 1 db, then why not use that class?  At
 least then there's the php manual to figure out what something does.

 Because then to add query logging for the whole app, you just need to put it
 in the class :)

 (I've done that before to check what's being run and where from, comes in
 very handy).

 --
 Postgresql  php tutorials
 http://www.designmagick.com/



That's done by tail -f /var/log/mysql/query.log. :D

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Chris



Yea if you're only targeting 1 db, then why not use that class?  At
least then there's the php manual to figure out what something does.

Because then to add query logging for the whole app, you just need to put it
in the class :)

(I've done that before to check what's being run and where from, comes in
very handy).





That's done by tail -f /var/log/mysql/query.log. :D


That won't tell you where a query comes from ;) Add a debug_backtrace 
into the class to also pinpoint where the query was called from. 
Complicated queries built on variables (or even just long queries built 
over multiple lines) will be hard to find just by looking at the mysql 
query log.


--
Postgresql  php tutorials
http://www.designmagick.com/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Eric Butera
On Wed, Jan 21, 2009 at 6:09 PM, Chris dmag...@gmail.com wrote:

 Yea if you're only targeting 1 db, then why not use that class?  At
 least then there's the php manual to figure out what something does.

 Because then to add query logging for the whole app, you just need to put
 it
 in the class :)

 (I've done that before to check what's being run and where from, comes in
 very handy).



 That's done by tail -f /var/log/mysql/query.log. :D

 That won't tell you where a query comes from ;) Add a debug_backtrace into
 the class to also pinpoint where the query was called from. Complicated
 queries built on variables (or even just long queries built over multiple
 lines) will be hard to find just by looking at the mysql query log.

 --
 Postgresql  php tutorials
 http://www.designmagick.com/



I've been using it for 5 years now and haven't had problems.  Then
again I still write sql by hand too.  I only use it when something is
acting really weird and I'm having a hard time.  So it is a targeted
process where I know what I'm looking for.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Jochem Maas
Chris schreef:
 
 Yea if you're only targeting 1 db, then why not use that class?  At
 least then there's the php manual to figure out what something does.
 Because then to add query logging for the whole app, you just need to
 put it
 in the class :)

 (I've done that before to check what's being run and where from,
 comes in
 very handy).

 

 That's done by tail -f /var/log/mysql/query.log. :D
 
 That won't tell you where a query comes from ;) Add a debug_backtrace
 into the class to also pinpoint where the query was called from.
 Complicated queries built on variables (or even just long queries built
 over multiple lines) will be hard to find just by looking at the mysql
 query log.
 

besides on shared hosting that log is often turned off even if you can get at 
it.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Eric Butera
On Wed, Jan 21, 2009 at 6:37 PM, Jochem Maas joc...@iamjochem.com wrote:
 Chris schreef:

 Yea if you're only targeting 1 db, then why not use that class?  At
 least then there's the php manual to figure out what something does.
 Because then to add query logging for the whole app, you just need to
 put it
 in the class :)

 (I've done that before to check what's being run and where from,
 comes in
 very handy).



 That's done by tail -f /var/log/mysql/query.log. :D

 That won't tell you where a query comes from ;) Add a debug_backtrace
 into the class to also pinpoint where the query was called from.
 Complicated queries built on variables (or even just long queries built
 over multiple lines) will be hard to find just by looking at the mysql
 query log.


 besides on shared hosting that log is often turned off even if you can get at 
 it.



That's why I set up a local dev environment.  If something is wrong,
just grab a db dump  figure it out locally.  That way I can do
whatever I need to really try out what the issue is and the best way
to resolve it.

Just merely saying how I develop.  Whatever gets it done is the real way. :)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Ashley Sheridan
On Wed, 2009-01-21 at 18:52 -0500, Eric Butera wrote:
 On Wed, Jan 21, 2009 at 6:37 PM, Jochem Maas joc...@iamjochem.com wrote:
  Chris schreef:
 
  Yea if you're only targeting 1 db, then why not use that class?  At
  least then there's the php manual to figure out what something does.
  Because then to add query logging for the whole app, you just need to
  put it
  in the class :)
 
  (I've done that before to check what's being run and where from,
  comes in
  very handy).
 
 
 
  That's done by tail -f /var/log/mysql/query.log. :D
 
  That won't tell you where a query comes from ;) Add a debug_backtrace
  into the class to also pinpoint where the query was called from.
  Complicated queries built on variables (or even just long queries built
  over multiple lines) will be hard to find just by looking at the mysql
  query log.
 
 
  besides on shared hosting that log is often turned off even if you can get 
  at it.
 
 
 
 That's why I set up a local dev environment.  If something is wrong,
 just grab a db dump  figure it out locally.  That way I can do
 whatever I need to really try out what the issue is and the best way
 to resolve it.
 
 Just merely saying how I develop.  Whatever gets it done is the real way. :)
 
A DB dump won't always tell you where the problem lies. With proper
logging, you can use the DB dump to work out what went wrong precisely.


Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] MySQL class. Thoughts?

2009-01-21 Thread Eric Butera
On Wed, Jan 21, 2009 at 7:07 PM, Ashley Sheridan
a...@ashleysheridan.co.uk wrote:
 On Wed, 2009-01-21 at 18:52 -0500, Eric Butera wrote:
 On Wed, Jan 21, 2009 at 6:37 PM, Jochem Maas joc...@iamjochem.com wrote:
  Chris schreef:
 
  Yea if you're only targeting 1 db, then why not use that class?  At
  least then there's the php manual to figure out what something does.
  Because then to add query logging for the whole app, you just need to
  put it
  in the class :)
 
  (I've done that before to check what's being run and where from,
  comes in
  very handy).
 
 
 
  That's done by tail -f /var/log/mysql/query.log. :D
 
  That won't tell you where a query comes from ;) Add a debug_backtrace
  into the class to also pinpoint where the query was called from.
  Complicated queries built on variables (or even just long queries built
  over multiple lines) will be hard to find just by looking at the mysql
  query log.
 
 
  besides on shared hosting that log is often turned off even if you can get 
  at it.
 
 

 That's why I set up a local dev environment.  If something is wrong,
 just grab a db dump  figure it out locally.  That way I can do
 whatever I need to really try out what the issue is and the best way
 to resolve it.

 Just merely saying how I develop.  Whatever gets it done is the real way. :)

 A DB dump won't always tell you where the problem lies. With proper
 logging, you can use the DB dump to work out what went wrong precisely.


 Ash
 www.ashleysheridan.co.uk



I meant grab a remote db dump, import it locally, reproduce the issue
locally, look at local query log, fix.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php