MySQLResultSet.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MySQLResultSet.php,v 1.22 2004/03/20 04:16:49 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/ResultSet.php'; 00023 require_once 'creole/common/ResultSetCommon.php'; 00024 00036 class MySQLResultSet extends ResultSetCommon implements ResultSet { 00037 00041 public function seek($rownum) 00042 { 00043 // MySQL rows start w/ 0, but this works, because we are 00044 // looking to move the position _before_ the next desired position 00045 if (!@mysql_data_seek($this->result, $rownum)) { 00046 return false; 00047 } 00048 $this->cursorPos = $rownum; 00049 return true; 00050 } 00051 00055 public function next() 00056 { 00057 $this->fields = mysql_fetch_array($this->result, $this->fetchmode); 00058 00059 if (!$this->fields) { 00060 $errno = mysql_errno($this->conn->getResource()); 00061 if (!$errno) { 00062 // We've advanced beyond end of recordset. 00063 $this->afterLast(); 00064 return false; 00065 } else { 00066 throw new SQLException("Error fetching result", mysql_error($this->conn->getResource())); 00067 } 00068 } 00069 00070 if (!$this->ignoreAssocCase) { 00071 $this->fields = array_change_key_case($this->fields, CASE_LOWER); 00072 } 00073 00074 // Advance cursor position 00075 $this->cursorPos++; 00076 return true; 00077 } 00078 00082 function getRecordCount() 00083 { 00084 $rows = @mysql_num_rows($this->result); 00085 if ($rows === null) { 00086 throw new SQLException("Error fetching num rows", mysql_error($this->conn->getResource())); 00087 } 00088 return (int) $rows; 00089 } 00090 00094 function close() 00095 { 00096 @mysql_free_result($this->result); 00097 $this->fields = array(); 00098 } 00099 00105 public function getString($column) 00106 { 00107 $idx = (is_int($column) ? $column - 1 : $column); 00108 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00109 if ($this->fields[$idx] === null) { return null; } 00110 return (string) $this->fields[$idx]; 00111 } 00112 00119 function getTimestamp($column, $format='Y-m-d H:i:s') 00120 { 00121 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00122 if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); } 00123 if ($this->fields[$column] === null) { return null; } 00124 00125 $ts = strtotime($this->fields[$column]); 00126 if ($ts === -1) { 00127 // otherwise it's an ugly MySQL timestamp! 00128 // YYYYMMDDHHMMSS 00129 if (preg_match('/([\d]{4})([\d]{2})([\d]{2})([\d]{2})([\d]{2})([\d]{2})/', $this->fields[$column], $matches)) { 00130 // YYYY MM DD HH MM SS 00131 // $1 $2 $3 $4 $5 $6 00132 $ts = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); 00133 } 00134 } 00135 if ($ts === -1) { // if it's still -1, then there's nothing to be done; use a different method. 00136 throw new SQLException("Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$column]); 00137 } 00138 if ($format === null) { 00139 return $ts; 00140 } 00141 if (strpos($format, '%') !== false) { 00142 return strftime($format, $ts); 00143 } else { 00144 return date($format, $ts); 00145 } 00146 } 00147 00148 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS