Jump to content
MakeWebGames

Sick & Tired


Haunted Dawg

Recommended Posts

Guest Anonymous

Re: Sick & Tired

 

Why submit 2 scripts 1 for quality and readability and 1 for spped & size one not just combine it.

Simple:

 

<?php
class CDatabase{var $a=0,$e=0,$m=null,$l=null;function CDatabase($s,$u,$p,$d){$this
->l=@mysql_connect($h,$u,$p);if(!is_resource($this->l))die("connect failed");if
(!@mysql_select_db($d,$this->l)){mysql_close($this->l);die("select failed");}}
function affectedRows(){return $this->a;}function errorMsg(){return $this->m;}
function errorNo(){return $this->e;}function &execute($q){$r=@mysql_query($q,
$this->l);if($r===false)return $this->raiseError();$this->a=mysql_affected_rows
($this->l);return $r;}function fetchAll($q){$h=&$this->execute($q);if(!
is_resource($h))return $h;$o=array();while($r=mysql_fetch_assoc($h))$o[]=$r;
mysql_free_result($h);return $o;}function fetchOne($q){if(is_array($r=$this->
fetchRow($q)))return array_shift(array_values($r));return $r;}function fetchRow
($q){$h=&$this->execute($q);if(!is_resource($h))return $h;$o=mysql_fetch_assoc(
$h);mysql_free_result($h);return $o;}function nextID($s){if($this->execute(
sprintf("UPDATE `%s` SET `id`=LAST_INSERT_ID(`id`+1)",$s)))return
mysql_insert_id($this->l);if(mysql_errno($this->l)==1146)if($this->execute(
sprintf("CREATE TABLE `%s` (`id` INT UNSIGNED NOT NULL)",$s)))if($this->
execute(sprintf("INSERT INTO `%s` VALUES (1)",$s)))return 1;return $this->
raiseError();}function quote($s){return is_null($s)?"NULL":sprintf("'%s'",
mysql_real_escape_string($s));}function &raiseError(){static $false=false;$this
->e=mysql_errno($this->l);$this->m=mysql_error($this->l);return $false;}}
?>

 

vs.

 

<?php
/**
**
**  CDatabase class
**  MySQL database driver for PHP 4+
**
**  Copyright (C) 2004-2008 Nyna <[email protected]>
**  All Rights Reserved.
**
**  ----
**
**  Redistribution and use in source and binary forms, with or without
**  modification, are permitted provided that the following conditions are
**  met:
**
**  * Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
**
**  * Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
**
**  * Neither the name Nyna nor the names of her contributors may be used to
**    endorse or promote products derived from this software without specific
**    prior written permission.
**
**  THIS SOFTWARE IS PROVIDED BY Nyna "AS IS" AND ANY EXPRESS OR IMPLIED
**  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
**  EVENT SHALL Nyna BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
**  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
**  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
**  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
**  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
**  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
**  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
**  ----
**
**  This software has been released solely onto the CE-PHP Games Forums by
**  Nyna. Anyone wishing to use it may do so AT THEIR OWN RISK.
**
**  Third party organizations may NOT offer this software for sale in any form
**  nor may they publish this software in any media including but not limited
**  to print and electronic form such as forums or file sharing systems
**  without explicit written permission from Nyna.
**
**  Requests for modification to this file will not be honoured, unless they
**  are to correct any bugs that I may have missed.
**
**/

/**
**
**  CDatabase
**
**  MySQL database driver
**
**/
class CDatabase
{
var $_affected = 0;
var $_errno    = 0;
var $_error    = null;
var $_link     = null;

/**
 **
 **  Constructor
 **
 **  Parameters:
 **
 **    <string> $server [hostname]|[ipaddress][:port]
 **    <string> $username
 **    <string> $password
 **    <string> $database
 **
 **  Visibility:
 **
 **    Public
 **
 **/
function CDatabase( $server, $username, $password, $database )
{
	// attempt to connect to the actual server

	$this->_link = @mysql_connect($server, $username, $password);

	if (!is_resource($this->_link))
	{
		// somethings wrong...

		die("Cannot connect to MySQL server");
	}

	// now select the appropriate database...

	if (!@mysql_select_db($database, $this->_link))
	{
		mysql_close($this->_link);

		die("Cannot select database");
	}
}

/**
 **
 **  affectedRows( )
 **
 **  Returns the number of rows affected by the most recent operation.
 **
 **  Parameters:
 **
 **    None
 **
 **  Returns:
 **
 **    <integer> $rows
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **/
function affectedRows( )
{
	return $this->_affected;
}

/**
 **
 **  errorMsg( )
 **
 **  Returns the most recent error message generated by a faulty statement.
 **
 **  Parameters:
 **
 **    None
 **
 **  Returns:
 **
 **    <string> $error
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **/
function errorMsg( )
{
	return $this->_error;
}

/**
 **
 **  errorNo( )
 **
 **  Returns the most recent error number generated by a faulty statement.
 **
 **  Parameters:
 **
 **    None
 **
 **  Returns:
 **
 **    <integer> $errno
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **/
function errorNo( )
{
	return $this->_errno;
}

/**
 **
 **  execute( )
 **
 **  Executes the specified query
 **
 **  Parameters:
 **
 **    <string> $sql
 **
 **  Returns:
 **
 **    <mixed> (See notes)
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **  Notes:
 **
 **    This function will return false on error, true for UPDATE, INSERT
 **    etc operations and a resource handle for SELECT operations.
 **
 **/
function &execute( $sql )
{
	// run the query

	$rs = @mysql_query($sql, $this->_link);

	// check for the obvious bad result

	if ($rs === false)
	{
		return $this->raiseError();
	}

	// set the affected rows

	$this->_affected = @mysql_affected_rows($this->_link);

	// and return either true or a resource handle

	return $rs;
}

/**
 **
 **  fetchAll( )
 **
 **  Returns all matching rows for the specified query in an array of
 **  of associative arrays.
 **
 **  Parameters:
 **
 **    <string> $sql
 **
 **  Returns:
 **
 **    <array> $rows
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **/
function fetchAll( $sql )
{
	$rs = &$this->execute($sql);

	if (!is_resource($rs))
	{
		return $rs;
	}

	$rows = array();

	while ($row = mysql_fetch_assoc($rs))
	{
		$rows[] = $row;
	}

	mysql_free_result($rs);

	return $rows;
}

/**
 **
 **  fetchOne( )
 **
 **  Returns a single column result from the specified query.
 **
 **  Parameters:
 **
 **    <string> $sql
 **
 **  Returns:
 **
 **    <string> $column, null if no records were found or false on error.
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **  Notes:
 **
 **    This function will usually (although not always) be called with a
 **    SELECT query.
 **
 **    Warning, the result of this may be confused with no matching records
 **    due to the return of a null.
 **
 **/
function fetchOne( $sql )
{
	// read the row

	if (is_array($row = $this->fetchRow($sql)))
	{
		// and return the first column

		return array_shift(array_values($row));
	}

	// either an error, or no rows were found.

	return $row;
}

/**
 **
 **  fetchRow( )
 **
 **  Returns a single row from the specified query in an associative array.
 **
 **  Parameters:
 **
 **    <string> $sql
 **
 **  Returns:
 **
 **    <array> $row, null if now rows found, or false on error
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **  Notes:
 **
 **    This function will usually (although not always) be called with a
 **    SELECT query.
 **
 **/
function fetchRow( $sql )
{
	$rs = &$this->execute($sql);

	if (!is_resource($rs))
	{
		// Dam, execute() failed, this will return false

		return $rs;
	}

	if (($rows = mysql_num_rows($rs)) === false)
	{
		// odd, mysql_num_rows() returned false, better return false

		$row = $this->raiseError();
	}
	else if (!$rows)
	{
		// no matchind rows found, return null

		$row = null;
	}
	else
	{
		// grab the row

		$row = mysql_fetch_assoc($rs);
	}

	// free the resource handle

	mysql_free_result($rs);

	// and return the result

	return $row;
}

/**
 **
 **  nextID( )
 **
 **  Used instead of auto-increment fields, nextID returns the next avaiable
 **  ID# from the specified table (creating it if necessary).
 **
 **  Parameters:
 **
 **    <string> $sequence_table_name
 **
 **  Returns:
 **
 **    <integer> $nextID or false on failure
 **
 **	 Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **  Notes:
 **
 **    Do NOT use this function inside a transaction unless the table
 **    already exists.
 **
 **    This operation IS however atomic, so can be safely called by multiple
 **    threads without the risk of collision.
 **
 **/
function nextID( $seq )
{
	// try to increment the last known value

	if ($this->execute(sprintf("UPDATE `%s` SET `id` = LAST_INSERT_ID(`id` + 1)", $seq)))
	{
		// good, that worked

		return mysql_insert_id($this->_link);
	}

	if (mysql_errno($this->_link) == 1146)
	{
		// table does not exist, so create it on the fly

		if ($this->execute(sprintf("CREATE TABLE `%s` (`id` INT UNSIGNED NOT NULL, PRIMARY KEY(`id`))", $seq)))
		{
			// insert our first value - 1

			if ($this->execute(sprintf("INSERT INTO `%s` VALUES (1)", $seq)))
			{
				// everything okay so far

				return 1;
			}
		}
	}

	// no choice but to return an error

	return $this->raiseError();
}

/**
 **
 **  quote( )
 **
 **  Used to protect the database against SQL injection. When passed a
 **  string, quote() returns the safely escaped string surrounded by single
 **  quotation marks. All other data is returned as is, except null which
 **  is return as NULL.
 **
 **  Parameters:
 **
 **    <mixed> $data
 **
 **  Returns:
 **
 **    <mixed> $safe_data
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility:
 **
 **    Public
 **
 **/
function quote( $data )
{
	// handle nulls

	if (is_null($data))
	{
		return "NULL";
	}

	// handle strings

	if (is_string($data))
	{
		return sprintf("'%s'", mysql_real_escape_string($data));
	}

	// all others are just returned as-is

	return $data;			
}

/**
 **
 **  raiseError( )
 **
 **  On an error being detected, raiseError is called setting the two
 **  internal variables (errno & error).
 **
 **  Parameters:
 **
 **    None
 **
 **  Returns:
 **
 **   <boolean> false
 **
 **  Throws:
 **
 **    Nothing
 **
 **  Visibility
 **
 **    Private
 **
 **/
function &raiseError()
{
	static $false = false;

	// set the error details

	$this->_errno = mysql_errno($this->_link);
	$this->_error = mysql_error($this->_link);

	// and return false

	return $false;
}
}

?>

 

These are not identical code-wise, but they perform the same operation. Were we looking at this issue - the code would have to perform 100% identically.

Link to comment
Share on other sites

Re: Sick & Tired

Hmm...I honestly don't see why my name would be in that list. I'm not that good of a coder. I've only been coding PHP for a month and a bit, so I'm still learning.

I've only made a few mods and edited a few.

I'm struggling with one at the moment! :-P

 

In my honest opinion, I'm no match for most of the coders/programmers on that list and therefore wouldn't really suit the competition, if there was to be one. ;)

Link to comment
Share on other sites

Re: Sick & Tired

 

Well, bullet said to me on msn, "war_hero is better than you".

I can't possibly see why he'd say that, as you've been coding for much longer than I have. In my own honest opinion, I still think I'm a newbie to PHP, as I'm still learning. I've got the basics and what-not, just developing my skills to a higher level. ;)

But as you clearly said (dementor), it doesn't really matter who's better than who. ;)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...