/** * 绑定参数 * @return bool * @throws \Exception */ public function bind() { $args_num = func_num_args(); if ($args_num == 0) { return false; } $types = $this->types; if (count($types) != $args_num) { throw new \Exception('arguments not equal the defined'); } $args = func_get_args(); $mysql = $this->mysql->connect(); $sql = $this->sql; foreach ($types as $k => $type) { switch ($type) { case 's': $v = $mysql->escape_string($args[$k]); $sql = preg_replace('/\\?/', '"' . $v . '"', $sql, 1); break; case 'i': $v = $mysql->escape_string($args[$k]); $sql = preg_replace('/\\?/', $v, $sql, 1); break; case 'd': $v = $mysql->escape_string($args[$k]); $sql = preg_replace('/\\?/', $v, $sql, 1); break; case 'b': break; } } $this->prepare = $sql; return true; }
/** * 绑定数据 * * @return bool|mixed */ public function bind() { if (func_num_args() == 0) { return false; } $args = func_get_args(); #绑定数据 $type = join('', $this->types[1]); array_unshift($args, $type); $mysql = $this->mysql->connect(); $prepare = $mysql->prepare($this->sql); if (!$prepare) { return false; } $this->prepare =& $prepare; //使用反射,避免参数引用错误 $method = new \ReflectionMethod($prepare, 'bind_param'); return $method->invokeArgs($prepare, $args); }
/** * 释放资源 */ public function free() { if ($this->result) { $this->result->free(); $mysql = $this->mysql->connect(); while ($mysql->more_results() && $mysql->next_result()) { $result = $mysql->store_result(); if ($result) { $result->free(); } } } }
<?php /** * @author Christian Rizza * www.crizza.com * email: christian@crizza.com */ session_start(); define('BASE_PATH', "./"); include BASE_PATH . "config.php"; if (!isset($_SESSION['admin'])) { header("Location: index.php"); } $conn = new MysqlConnector(); $conn->connect(); $student = new Student(); $student->setConnector($conn); $lista = $student->getList(); foreach ($lista as $entry) { echo "Email: " . $entry->email . " "; $entry->setConnector($conn); if ($entry->generatePassword()) { echo "[Errore di invio] <br>"; } else { echo "[OK] <br>"; } $counter++; } $conn->disconnect(); ?>