MySQLResultSet.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MySQLResultSet.php,v 1.2 2004/03/29 18:46:46 micha 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 // 00023 // STATUS: 00024 // - ported: y 00025 // - exceptions: y 00026 // - compiled: y 00027 // - tested: n 00028 // 00029 00030 require_once 'creole/common/ResultSetCommon.php'; 00031 00043 class MySQLResultSet extends ResultSetCommon 00044 { 00048 function seek($rownum) 00049 { 00050 // MySQL rows start w/ 0, but this works, because we are 00051 // looking to move the position _before_ the next desired position 00052 if (!@mysql_data_seek($this->result, $rownum)) { 00053 return false; 00054 } 00055 $this->cursorPos = $rownum; 00056 return true; 00057 } 00058 00059 00063 function next() 00064 { 00065 $this->fields = mysql_fetch_array($this->result, $this->fetchmode); 00066 00067 if (!$this->fields) { 00068 $errno = mysql_errno($this->conn->getResource()); 00069 if (!$errno) { 00070 // We've advanced beyond end of recordset. 00071 $this->afterLast(); 00072 return false; 00073 } 00074 else { 00075 return new SQLException(CREOLE_ERROR, "Error fetching result", mysql_error($this->conn->getResource())); 00076 } 00077 } 00078 00079 if (!$this->ignoreAssocCase) { 00080 $this->fields = array_change_key_case($this->fields, CASE_LOWER); 00081 } 00082 00083 // Advance cursor position 00084 $this->cursorPos++; 00085 return true; 00086 } 00087 00091 function getRecordCount() 00092 { 00093 $rows = @mysql_num_rows($this->result); 00094 if ($rows === null) { 00095 return new SQLException(CREOLE_ERROR, "Error fetching num rows", mysql_error($this->conn->getResource())); 00096 } 00097 return (int) $rows; 00098 } 00099 00103 function close() 00104 { 00105 @mysql_free_result($this->result); 00106 $this->fields = array(); 00107 } 00108 00114 function getString($column) 00115 { 00116 $idx = (is_int($column) ? $column - 1 : $column); 00117 if (!array_key_exists($idx, $this->fields)) { 00118 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00119 } 00120 if ($this->fields[$idx] === null) { return null; } 00121 return (string) $this->fields[$idx]; 00122 } 00123 00130 function getTimestamp($column, $format='Y-m-d H:i:s') 00131 { 00132 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00133 if (!array_key_exists($column, $this->fields)) { 00134 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); 00135 } 00136 00137 if ($this->fields[$column] === null) { return null; } 00138 00139 $ts = strtotime($this->fields[$column]); 00140 if ($ts === -1) { 00141 // otherwise it's an ugly MySQL timestamp! 00142 // YYYYMMDDHHMMSS 00143 if (preg_match('/([\d]{4})([\d]{2})([\d]{2})([\d]{2})([\d]{2})([\d]{2})/', $this->fields[$column], $matches)) { 00144 // YYYY MM DD HH MM SS 00145 // $1 $2 $3 $4 $5 $6 00146 $ts = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); 00147 } 00148 } 00149 if ($ts === -1) { // if it's still -1, then there's nothing to be done; use a different method. 00150 return new SQLException(CREOLE_ERROR_INVALID, "Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$column]); 00151 } 00152 if ($ts === null) { 00153 return $ts; 00154 } 00155 if (strpos($format, '%') !== false) { 00156 return strftime($format, $ts); 00157 } else { 00158 return date($format, $ts); 00159 } 00160 } 00161 00162 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS