$tag = 1; $isbn = $_GET['isbn']; //连接数据库模块 $serverName = "localhost"; $uid = "sa"; $pwd = "123456"; $connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database" => "MBOOK"); $conn = sqlsrv_connect($serverName, $connectionInfo); sqlsrv_query($conn, "set names GB2312"); $sql = "SELECT * FROM TBook WHERE TBook.ISBN='{$isbn}'"; $result = sqlsrv_query($conn, $sql); $row = sqlsrv_fetch($result); $book_name = sqlsrv_get_field($result, 1); $author = sqlsrv_get_field($result, 2); $publisher = sqlsrv_get_field($result, 3); $publish_date = sqlsrv_get_field($result, 4, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); $price = sqlsrv_get_field($result, 5); $fuben_num = sqlsrv_get_field($result, 6); $store_num = sqlsrv_get_field($result, 7); $type_num = sqlsrv_get_field($result, 8); $content_view = sqlsrv_get_field($result, 9); $sql = "SELECT * FROM TBLend WHERE ISBN='{$isbn}' AND 是否借出=0"; $result = sqlsrv_query($conn, $sql); require_once "top_menu.php"; ?> <!--内容模块--> <div class="container"> <div class="content_display_book"> <table class="table table-bordered"> <thead> <th colspan="3"><p class="text-center">图书信息表</p></th>
/** * @brief 결과를 fetch **/ function _fetch($result) { if (!$this->isConnected() || $this->isError() || !$result) { return; } $c = sqlsrv_num_fields($result); $m = null; $output = array(); while (sqlsrv_fetch($result)) { if (!$m) { $m = sqlsrv_field_metadata($result); } unset($row); for ($i = 0; $i < $c; $i++) { $row->{$m[$i]['Name']} = sqlsrv_get_field($result, $i, SQLSRV_PHPTYPE_STRING('utf-8')); } $output[] = $row; } if (count($output) == 1) { return $output[0]; } return $output; }
/** * Low level handling getting a row from a result set; automatically * makes all fetched values strings, just like the other PHP db functions. * We have to do this since the sqlsrv extension returns row values in thier * native types, which causes problems with how we handle things. * * @param resource $result * @return array */ private function _fetchRowAssoc($result) { if (!is_resource($result)) { return false; } $row = array(); $fieldnames = $this->getFieldsArray($result); $fieldMetaData = sqlsrv_field_metadata($result); if (sqlsrv_fetch($result)) { for ($i = 0; $i < sqlsrv_num_fields($result); $i++) { if ($fieldMetaData[$i]['Type'] == -9 || $fieldMetaData[$i]['Type'] >= SQLSRV_SQLTYPE_NVARCHAR(1) && $fieldMetaData[$i]['Type'] <= SQLSRV_SQLTYPE_NVARCHAR(8000) || $fieldMetaData[$i]['Type'] >= SQLSRV_SQLTYPE_NCHAR(1) && $fieldMetaData[$i]['Type'] <= SQLSRV_SQLTYPE_NCHAR(8000) || $fieldMetaData[$i]['Type'] == SQLSRV_SQLTYPE_NVARCHAR('max') || $fieldMetaData[$i]['Type'] == SQLSRV_SQLTYPE_NCHAR('max')) { $row[$fieldnames[$i]] = iconv("utf-16le", "utf-8", sqlsrv_get_field($result, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY))); } else { $row[$fieldnames[$i]] = sqlsrv_get_field($result, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); } } } else { sqlsrv_free_stmt($result); } return $row; }
public function debugTable() { /* Retrieve the number of fields. */ $numFields = sqlsrv_num_fields($this->result); echo '<table>'; echo '<tr><td colspan="' . $numFields . '">' . $this->currentQuery . '</td></tr>'; /* Iterate through each row of the result set. */ do { echo '<tr>'; /* Iterate through the fields of each row. */ for ($i = 0; $i < $numFields; $i++) { echo '<td>' . sqlsrv_get_field($this->result, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)) . '</td>'; } echo '</tr>'; echo "\n"; } while (sqlsrv_fetch($this->result)); echo '</table>'; // Avoir un objet connexion et reeécuter la requete }
protected function getSqlType() { $sqlType = null; $phpType = null; switch ($this->type) { case SqlParameterTypes::SqlInt: $sqlType = SQLSRV_SQLTYPE_INT; $phpType = SQLSRV_PHPTYPE_INT; break; case SqlParameterTypes::SqlBigInt: $sqlType = SQLSRV_SQLTYPE_BIGINT; $phpType = SQLSRV_PHPTYPE_INT; break; case SqlParameterTypes::SqlBit: $sqlType = SQLSRV_SQLTYPE_BIT; $phpType = SQLSRV_PHPTYPE_INT; break; case SqlParameterTypes::SqlFloat: $sqlType = SQLSRV_SQLTYPE_FLOAT; $phpType = SQLSRV_PHPTYPE_FLOAT; break; case SqlParameterTypes::SqlDecimal: $sqlType = SQLSRV_SQLTYPE_DECIMAL($this->precision, $this->scale); $phpType = SQLSRV_PHPTYPE_FLOAT; break; case SqlParameterTypes::SqlNVarChar: $sqlType = SQLSRV_SQLTYPE_NVARCHAR($this->size); $phpType = SQLSRV_PHPTYPE_STRING("UTF-8"); break; case SqlParameterTypes::SqlNVarCharMax: $sqlType = SQLSRV_SQLTYPE_NVARCHAR('max'); $phpType = SQLSRV_PHPTYPE_STRING("UTF-8"); break; case SqlParameterTypes::SqlXml: $sqlType = SQLSRV_SQLTYPE_XML; $phpType = SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY); break; case SqlParameterTypes::SqlUniqueIdentifier: $sqlType = SQLSRV_SQLTYPE_UNIQUEIDENTIFIER; $phpType = SQLSRV_PHPTYPE_STRING("UTF-8"); break; case SqlParameterTypes::SqlTinyInt: $sqlType = SQLSRV_SQLTYPE_TINYINT; $phpType = SQLSRV_PHPTYPE_INT; break; case SqlParameterTypes::SqlDate: $sqlType = SQLSRV_SQLTYPE_DATE; $phpType = SQLSRV_PHPTYPE_DATETIME; break; case SqlParameterTypes::SqlDateTime: $sqlType = SQLSRV_SQLTYPE_DATETIME; $phpType = SQLSRV_PHPTYPE_DATETIME; break; case SqlParameterTypes::SqlTime: $sqlType = SQLSRV_SQLTYPE_TIME; $phpType = SQLSRV_PHPTYPE_DATETIME; break; case SqlParameterTypes::SqlTimestamp: $sqlType = SQLSRV_SQLTYPE_TIMESTAMP; $phpType = SQLSRV_PHPTYPE_DATETIME; break; } return array($sqlType, $phpType); }
<?php include 'DBConnection.php'; $lng_min = $_REQUEST['lng_min']; $lat_min = $_REQUEST['lat_min']; $lng_max = $_REQUEST['lng_max']; $lat_max = $_REQUEST['lat_max']; $tsql = "SELECT event_id, latitude, longtitude, name, working_on, looking_for FROM event WHERE longtitude >= {$lng_min} AND longtitude <= {$lng_max} AND latitude >= {$lat_min} AND latitude <= {$lat_max}"; $stmt = sqlsrv_query($conn, $tsql); if ($stmt === false) { echo "Error in statement preparation/execution.\n"; die(print_r(sqlsrv_errors(), true)); } echo '['; $is_first = 1; while (sqlsrv_fetch($stmt)) { $event_id = sqlsrv_get_field($stmt, 0); $lat = sqlsrv_get_field($stmt, 1); $lng = sqlsrv_get_field($stmt, 2); $name = sqlsrv_get_field($stmt, 3, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); $working_on = sqlsrv_get_field($stmt, 4, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); $looking_for = sqlsrv_get_field($stmt, 5, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); if ($is_first == 0) { echo ","; } echo "[\"{$event_id}\", \"{$lat}\",\"{$lng}\",\"{$name}\", \"{$working_on}\", \"{$looking_for}\"]"; $is_first = 0; } echo ']'; include 'DBConnectionClose.php';
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database" => "MBOOK"); $conn = sqlsrv_connect($serverName, $connectionInfo); sqlsrv_query($conn, "set names GB2312"); $sql = "SELECT * FROM TReader WHERE 借书证号='{$lend_id}'"; $result = sqlsrv_query($conn, $sql); $row = sqlsrv_fetch($result); $password = sqlsrv_get_field($result, 1); $name = sqlsrv_get_field($result, 2); $sex = sqlsrv_get_field($result, 3); $sex = $sex == 1 ? "男" : "女"; $birthdate = sqlsrv_get_field($result, 4, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); $major = sqlsrv_get_field($result, 5); $book_lend_num = sqlsrv_get_field($result, 6); $photo = sqlsrv_get_field($result, 7); $comment = sqlsrv_get_field($result, 8); $contact = sqlsrv_get_field($result, 9, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); sqlsrv_close($conn); require_once "top_menu.php"; ?> <!--内容模块--> <div class="container"> <div class="content_update_user span10 offset1"> <form class="form-horizontal" name="update_reader" method="post" enctype="multipart/form-data" action="fun.php?type=admin&cmd=update_user"> <div class="control-group"> <label class="control-label" for="len_num">借书证号</label> <div class="controls"> <input class="uneditable-input" readonly type="text" id="len_num" name="len_num" value="<?php echo $lend_id; ?> "> </div>
/** * Fetch a result row as a numeric array * @param Mixed qHanle The query handle * @return Array */ public function fetch_numarray($qHanle) { $rowArray = array(); $metaData = sqlsrv_field_metadata($qHanle); $fetchRes = sqlsrv_fetch($qHanle); if ($fetchRes === false) { triggerErrorMSSQL(); return $rowArray; } if (is_null($fetchRes)) { return $rowArray; } $j = 0; foreach ($metaData as $fieldMetadata) { switch ($fieldMetadata['Type']) { //dateTime case 93: $fieldVal = sqlsrv_get_field($qHanle, $j, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); $fieldVal = substr($fieldVal, 0, strrpos($fieldVal, ".")); break; // ntext // ntext case -10: $fieldVal = sqlsrv_get_field($qHanle, $j, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR)); $buffer = null; while (!feof($fieldVal)) { $buffer .= fgets($fieldVal, 4096); } fclose($fieldVal); $fieldVal = $buffer; break; // image // image case -4: $fieldVal = sqlsrv_get_field($qHanle, $j, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY)); $buffer = null; while (!feof($fieldVal)) { $buffer .= fgets($fieldVal, 4096); } fclose($fieldVal); $fieldVal = $buffer; break; // text // text case -1: $fieldVal = sqlsrv_get_field($qHanle, $j, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR)); $buffer = null; while (!feof($fieldVal)) { $buffer .= fgets($fieldVal, 4096); } fclose($fieldVal); $fieldVal = $buffer; break; // need to check, may be int data should be retrieved in another type // need to check, may be int data should be retrieved in another type default: $fieldVal = sqlsrv_get_field($qHanle, $j); } $rowArray[] = $fieldVal; $j++; } return $rowArray; }
/** * Sets up a prepared statement * * @internal * * @param fDatabase $database The database object this result set was created from * @param string $query The SQL statement to prepare * @param array $placeholders The data type placeholders * @param string $untranslated_query The original untranslated SQL, if applicable * @return fStatement */ public function __construct($database, $query, $placeholders, $untranslated_sql) { if (!$database instanceof fDatabase) { throw new fProgrammerException('The database object provided does not appear to be a descendant of fDatabase'); } $this->database = $database; $this->placeholders = $placeholders; $this->sql = vsprintf($query, $placeholders); $this->untranslated_sql = $untranslated_sql; $extension = $this->database->getExtension(); if ($extension == 'pdo' && $this->database->getType() == 'mssql') { $extension = 'pdo_dblib'; } switch ($extension) { // These database extensions don't have prepared statements case 'mssql': case 'mysql': case 'pdo_dblib': case 'sqlite': $query = vsprintf($query, $placeholders); break; case 'oci8': $named_placeholders = array(); for ($i = 1; $i <= sizeof($placeholders); $i++) { $named_placeholders[] = ':p' . $i; } $query = vsprintf($query, $named_placeholders); break; case 'ibm_db2': case 'mysqli': case 'pdo': case 'sqlsrv': $question_marks = array(); if (sizeof($placeholders)) { $question_marks = array_fill(0, sizeof($placeholders), '?'); } $query = vsprintf($query, $question_marks); break; case 'pgsql': $dollar_placeholders = array(); for ($i = 1; $i <= sizeof($placeholders); $i++) { $dollar_placeholders[] = '$' . $i; } $query = vsprintf($query, $dollar_placeholders); break; } $connection = $this->database->getConnection(); fCore::startErrorCapture(E_WARNING); switch ($extension) { // These database extensions don't have prepared statements case 'mssql': case 'mysql': case 'pdo_dblib': case 'sqlite': $statement = $query; break; case 'ibm_db2': $statement = db2_prepare($connection, $query, array('cursor' => DB2_FORWARD_ONLY)); break; case 'mysqli': $statement = mysqli_prepare($connection, $query); break; case 'oci8': $statement = oci_parse($connection, $query); break; case 'pdo': $statement = $connection->prepare($query); break; case 'pgsql': static $statement_number = 0; $statement_number++; $this->identifier = 'fstmt' . $statement_number; $statement = pg_prepare($connection, $this->identifier, $query); break; case 'sqlsrv': $params = array(); for ($i = 0; $i < sizeof($placeholders); $i++) { if ($placeholders[$i] == '%s') { $this->bound_params[$i] = array(NULL, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('UTF-8')); } else { $this->bound_params[$i] = array(NULL); } $params[$i] =& $this->bound_params[$i]; } $statement = sqlsrv_prepare($connection, $query, $params); break; } fCore::stopErrorCapture(); if (!$statement) { switch ($extension) { case 'ibm_db2': $message = db2_stmt_errormsg($statement); break; case 'mysqli': $message = mysqli_error($connection); break; case 'oci8': $error_info = oci_error($statement); $message = $error_info['message']; break; case 'pgsql': $message = pg_last_error($connection); break; case 'sqlsrv': $error_info = sqlsrv_errors(SQLSRV_ERR_ALL); $message = $error_info[0]['message']; break; case 'pdo': $error_info = $connection->errorInfo(); $message = $error_info[2]; break; } $db_type_map = array('db2' => 'DB2', 'mssql' => 'MSSQL', 'mysql' => 'MySQL', 'oracle' => 'Oracle', 'postgresql' => 'PostgreSQL', 'sqlite' => 'SQLite'); throw new fSQLException('%1$s error (%2$s) in %3$s', $db_type_map[$this->database->getType()], $message, $this->sql); } $this->statement = $statement; }
<table class="table table-hover table-bordered"> <thead> <th>图书ISBN</th> <th>图书ID</th> <th>借书证号</th> <th>借书时间</th> <th>应还时间</th> </thead> <tbody> <?php while ($row = sqlsrv_fetch($result)) { $lend_id = sqlsrv_get_field($result, 1); $isbn = sqlsrv_get_field($result, 2); $book_id = sqlsrv_get_field($result, 3); $borrow_time = sqlsrv_get_field($result, 4, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); $return_time = sqlsrv_get_field($result, 5, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); echo "<tr>"; echo "<td>" . $isbn . "</td>" . "<td>" . $book_id . "</td>" . "<td>" . $lend_id . "</td>" . "<td>" . $borrow_time . "</td>" . "<td class=\" text-info\">" . $return_time . "</td>"; echo "</tr>"; } ?> </tbody> </table> <?php ?> </div> </div> <!--也页脚模块--> <div class="container footer">
/** * Write session data. * * @param string A session ID. * @param string A serialized chunk of session data. * * @return bool true, if the session was written^successfully. * * @throws AgaviStorageException If the session data cannot be written. * * @author David Zülke <*****@*****.**> * @since 1.0.6 */ public function sessionWrite($id, &$data) { if (!$this->connection) { return false; } // get table/column $db_table = $this->getParameter('db_table'); $db_data_col = $this->getParameter('db_data_col', 'sess_data'); $db_id_col = $this->getParameter('db_id_col', 'sess_id'); $db_time_col = $this->getParameter('db_time_col', 'sess_time'); $ts = date($this->getParameter('date_format', 'U')); if (is_numeric($ts)) { $ts = (int) $ts; } // attempt an update first $sql = sprintf("UPDATE %s SET %s = ?, %s = ? WHERE %s = ?", $db_table, $db_data_col, $db_time_col, $db_id_col); $result = sqlsrv_query($this->connection, $sql, array(array($data, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)), $ts, $id)); if ($result !== false && sqlsrv_rows_affected($result)) { return true; } elseif ($result !== false) { // no rows affected, so it's time for an insert $sql = sprintf("INSERT INTO %s (%s, %s, %s) VALUES(?, ?, ?)", $db_table, $db_data_col, $db_time_col, $db_id_col); $result = sqlsrv_query($this->connection, $sql, array(array($data, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)), $ts, $id)); if ($result !== false && sqlsrv_rows_affected($result)) { return true; } } else { // something went wrong $error = "AgaviSqlsrvSessionStorage cannot insert or update session, error reported by server:\n\n%s"; $error = sprintf($error, implode("\n", $this->getContext()->getDatabaseManager()->getDatabase($this->getParameter('database'))->getErrors())); throw new AgaviStorageException($error); } }