Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
This is the forum to discuss the Wrox book Professional PHP5 by Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove; ISBN: 9780764572821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 2nd, 2009, 12:57 PM
Registered User
 
Join Date: Jun 2008
Location: amuwo-odofin, lagos, Nigeria.
Posts: 7
Thanks: 5
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shegxzyl
Default MySQL version for sql.phpm [Chapter 7]

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

we then use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
so here goes the class...

Code:
class sql {
  private $result_rows;  # Result rows hash
  private $query_handle;  # db: the query handle
  private $link_ident;  # db: the link identifier
  private $last_id;		# last id. incase we need it for future use
  
  public function __construct() {

  	$db ="my_db";
	$db_host="localhost";
	$db_username="mysqluser";
	$db_password="mysqlpass";
	
	# create a database connection
    $this->link_ident = mysql_connect($db_host, $db_username, $db_password);
	if ( !is_resource($this->link_ident) )
		die ('Cannot connect to DB');
	else # select a database if the connection was successful
		mysql_select_db($db, $this->link_ident);
  }
  
  public function query($sql, $code_return_mode = 0) {
    $result = mysql_query( $sql, $this->link_ident ) or die( 'error: ' . mysql_error() );
	$mysql_last_id = mysql_insert_id($this->link_ident);    
	
	# INSERT, UPDATE, DELETE, DROP etc queries returns a boolean. only mysql_affected_rows can be used on this.
	# While SELECT, SHOW, DESCRIBE, EXPLAIN returns a resource. so we can apply mysql_num_rows.
	# check if the result is not a resource
	if ( !is_resource($result) ) {
		//do nothing
		$this->last_id = mysql_affected_rows($this->link_ident);
	} else {	# if it is a resource; we can then make use of mysql_num_rows

		for ( $i=0; $i <= mysql_num_rows($result) - 1; $i++ ) {
			$response = mysql_fetch_array($result, $i);
			$return_array[$i] = $response;
			$this->last_id = $mysql_last_id;
		};

	}

	    $this->result_rows = $return_array;
		
    if (!$result) 
	{
      error_log("QUERY FAILED: $sql\n");
    };
	
    if (!$result) 
	{
      return(1);
    } else 
	{
      return(0);  # return 0 if it fails
    };
  }
  
  public function get_result($row_num, $column_name) {
    return ($this->result_rows[$row_num][$column_name]);
  }
  
  public function get_row_hash($row_num) {
    return ($this->result_rows[$row_num]);
  }  
  
  public function get_table_hash() {
    return $this->result_rows;
  }
  # you can as well use this to get the last id of an insert sql statement.
  public function get_last_id() {
  	return $this->last_id;
	}
  
  public function done($close_connection = 0) {
    if ($close_connection) {
      mysql_close($this->link_ident);
    };
  }
};
it works fine. i hope its useful for somebody.
thanks to slurp56 for usersession.phpm mysql conversion.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql version nightwhisper09 PHP How-To 0 October 4th, 2009 06:46 PM
Chapter 15: usersession.phpm database conversion cashflowtips BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 August 9th, 2007 04:00 AM
Chapter 15: usersession.phpm mlange BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 12 May 4th, 2007 09:04 AM
How to update Mysql version qazi_nomi MySQL 1 September 8th, 2004 09:50 AM
How to update Mysql version qazi_nomi MySQL 1 June 14th, 2004 02:45 PM



All times are GMT -4. The time now is 01:10 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.