cvs: /php4/pear/DB mysql.php
| From: | Stig Bakken | Date: | Wed, 09 Feb 2000 19:03:56 +0000 |
| Subject: | cvs: /php4/pear/DB mysql.php | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-9989@lists.php.net to get a copy of this message | ||
ssb Wed Feb 9 11:03:56 2000 EDT
Modified files:
/php4/pear/DB mysql.php
Log:
Fixed a few bugs, especially in prepare/execute.
Introduced "get modes", letting you choose how fetched array data is indexed.
Index: php4/pear/DB/mysql.php
diff -u php4/pear/DB/mysql.php:1.8 php4/pear/DB/mysql.php:1.9
--- php4/pear/DB/mysql.php:1.8 Thu Feb 3 02:49:04 2000
+++ php4/pear/DB/mysql.php Wed Feb 9 11:03:26 2000
@@ -176,7 +176,7 @@
}
// Determine which queries that should return data, and which
// should return an error code only.
- if (stristr('SELECT', $query)) {
+ if (preg_match('/(SELECT|SHOW)/i', $query)) {
return $result;
} else {
return DB_OK;
@@ -190,13 +190,23 @@
* Fetch a row and return as array.
*
* @param $result MySQL result identifier
+ * @param $getmode how the resulting array should be indexed
*
- * @return int an array on success, a DB error code on failure
+ * @return int an array on success, a DB error code on failure, NULL
+ * if there is no more data
*/
- function fetchRow($result) {
- $row = mysql_fetch_row($result);
+ function fetchRow($result, $getmode = DB_GETMODE_DEFAULT) {
+ if ($getmode & DB_GETMODE_ASSOC) {
+ $row = mysql_fetch_array($result, MYSQL_ASSOC);
+ } else {
+ $row = mysql_fetch_row($result);
+ }
if (!$row) {
- return $this->errorCode(mysql_errno($this->connection));
+ $errno = mysql_errno($this->connection);
+ if (!$errno) {
+ return NULL;
+ }
+ return $this->errorCode($errno);
}
return $row; // XXX ADDREF
}
@@ -209,13 +219,22 @@
*
* @param $result MySQL result identifier
* @param $arr (reference) array where data from the row is stored
+ * @param $getmode how the array data should be indexed
*
* @return int DB_OK on success, a DB error code on failure
*/
- function fetchInto($result, &$arr) {
- $arr = mysql_fetch_row($result);
+ function fetchInto($result, &$arr, $getmode = DB_GETMODE_DEFAULT) {
+ if ($getmode & DB_GETMODE_ASSOC) {
+ $arr = mysql_fetch_array($result, MYSQL_ASSOC);
+ } else {
+ $arr = mysql_fetch_row($result);
+ }
if (!$arr) {
- return $this->errorCode(mysql_errno($this->connection));
+ $errno = mysql_errno($this->connection);
+ if (!$errno) {
+ return NULL;
+ }
+ return $this->errorCode($errno);
}
return DB_OK;
}
@@ -281,9 +300,7 @@
* MySQL, this is emulated.
*/
function prepare($query) {
- $t = &$this->prepare_tokens;
- $y = &$this->prepare_types;
- $tokens = split('[\*\?]', $query);
+ $tokens = split('[\&\?]', $query);
$token = 0;
$types = array();
for ($i = 0; $i < strlen($query); $i++) {
@@ -291,16 +308,15 @@
case '?':
$types[$token++] = DB_PARAM_SCALAR;
break;
- case '*':
+ case '&':
$types[$token++] = DB_PARAM_OPAQUE;
break;
}
}
- $t[] = $tokens;
- end($t);
- $k = key($t);
- $y[$k] = $types;
- print "<pre>";var_dump($tokens);var_dump($types);print
"</pre>\n";
+ $this->prepare_tokens[] = &$tokens;
+ end($this->prepare_tokens);
+ $k = key($this->prepare_tokens);
+ $this->prepare_types[$k] = $types;
return $k;
}
@@ -315,10 +331,12 @@
function execute($stmt, $data = false) {
$p = &$this->prepare_tokens;
$stmt = 0; // XXX HORRIBLE HACK
- if (!isset($p[$stmt]) || !is_array($p[$stmt]) || !sizeof($p[$stmt])) {
+ if (!isset($this->prepare_tokens[$stmt]) ||
+ !is_array($this->prepare_tokens[$stmt]) ||
+ !sizeof($this->prepare_tokens[$stmt])) {
return DB_ERROR_INVALID;
}
- $qq = &$p[$stmt];
+ $qq = &$this->prepare_tokens[$stmt];
$qp = sizeof($qq) - 1;
if ((!$data && $qp > 0) ||
(!is_array($data) && $qp > 1) ||
@@ -327,23 +345,34 @@
}
$realquery = $qq[0];
for ($i = 0; $i < $qp; $i++) {
- $realquery .= "'";
if ($this->prepare_types[$stmt][$i] == DB_PARAM_OPAQUE) {
- $fp = @fopen($data[$i], "r");
- $data = '';
+ if (is_array($data)) {
+ $fp = fopen($data[$i], "r");
+ } else {
+ $fp = fopen($data, "r");
+ }
+ $pdata = '';
if ($fp) {
- while ($buf = fread($fp, 4096)) {
- $data .= $buf;
+ while (($buf = fread($fp, 4096)) != false) {
+ $pdata .= $buf;
}
}
- . $this->quoteString($data[$i]) . "'";
+ } else {
+ if (is_array($data)) {
+ $pdata = &$data[$i];
+ } else {
+ $pdata = &$data;
+ }
+ }
+ $realquery .= "'" . $this->quoteString($pdata) . "'";
$realquery .= $qq[$i + 1];
}
+ print "realquery=";var_dump($realquery);
$result = mysql_query($realquery, $this->connection);
if (!$result) {
return $this->errorCode(mysql_errno($this->connection));
}
- if (stristr('SELECT', $realquery)) {
+ if (preg_match('/(SELECT|SHOW)/i', $realquery)) {
return $result;
} else {
return DB_OK;
@@ -383,7 +412,10 @@
// }}}
- // Methods still not implemented:
+ // TODO/wishlist:
+ // simpleFetch
+ // simpleGet
+ // affectedRows
// longReadlen
// binmode
}