php未定義數(shù)據(jù)庫類型 數(shù)據(jù)庫函數(shù)未定義

我在php里邊建了一個(gè)數(shù)組,然后想訪問數(shù)組中元素,服務(wù)器端能得到數(shù)據(jù),為什么頁面上顯示未定義

jsp一共有9個(gè)內(nèi)置對(duì)象,分別是以下9個(gè):

創(chuàng)新互聯(lián)公司是專業(yè)的坪山網(wǎng)站建設(shè)公司,坪山接單;提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行坪山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

1.Request對(duì)象

發(fā)送請(qǐng)求時(shí),該對(duì)象被創(chuàng)建,一次請(qǐng)求結(jié)束后即銷毀。(一次請(qǐng)求產(chǎn)生一個(gè)request對(duì)象)

該對(duì)象封裝了用戶提交的信息,通過調(diào)用該對(duì)象相應(yīng)的方法要以獲取封裝的信息。即,使用該對(duì)象可以獲取用戶提交的信息。

當(dāng)Request對(duì)象獲取客戶提交的內(nèi)容為漢字字符時(shí),會(huì)出現(xiàn)亂碼現(xiàn)象,則必須進(jìn)行特殊處理。首先,將獲取的字符串用ISO8859-1進(jìn)行編碼,并將編碼存放到一個(gè)字節(jié)數(shù)組中,然后將這個(gè)數(shù)組轉(zhuǎn)化為字符串對(duì)象即可。如下代碼:

String text = request.getParameter("body");

byte [] b = text.getBytes("ISO8859-1"); //將text字符串轉(zhuǎn)換成字節(jié)數(shù)組---編碼的過程

text = new String(b); //構(gòu)造字符串,按照當(dāng)前操作系統(tǒng)的字符集進(jìn)行構(gòu)造。將字節(jié)轉(zhuǎn)換成字符串---解碼的過程

Request常用的方法:

getParameter(String s) ----- 獲取表單提交的信息

String s = request.getParameter("name");

getProtocol() -----獲取客戶使用的協(xié)議

String s = request.getProtocol() ;

getServletPath() ----獲取客戶提交信息的頁面

String s = request.getServletPath();

getMethod() -----獲取客戶提交信息的方式 get或post

String s = request.getMethod();

getHeader() ----- 獲取HTTP頭文件中的accept、accept-encoding和host的值

String s = request.getHeader("accept");

getRemoteAddr() --獲取客戶的ip地址

String s = request.getRemoteAddr();

getServerHost() ---獲取客戶機(jī)的名稱

String s = request.getServerHost();

getServerName() ---獲取服務(wù)器名稱

String s = request.getServerName();

getServerPort() ----獲取服務(wù)器的商口號(hào)

String s = request.getServerPort();

getPamameterNames() ----獲取客戶端提交的所有參數(shù)的名字

Enumeration enum = request.getParameterNames();

while(enum.hasMoreElements()){

String s = (String) enum.nextElement();

out.println(s);

}

2. Response對(duì)象:對(duì)客戶的請(qǐng)求做出動(dòng)態(tài)響應(yīng),向客戶端發(fā)送數(shù)據(jù)。響應(yīng)客戶請(qǐng)求時(shí)創(chuàng)建,響應(yīng)結(jié)束時(shí)銷毀。(一次響應(yīng)產(chǎn)生一個(gè)response對(duì)象)

動(dòng)態(tài)響應(yīng)contentType屬性

當(dāng)一個(gè)用戶訪問了一個(gè)jsp頁面時(shí),如果該頁面用page指令設(shè)置頁面的contentType屬性為text/html,那么jsp引擎將按照這個(gè)屬性值做出反應(yīng)。如果要?jiǎng)討B(tài)改變這個(gè)屬性值來響應(yīng)客戶,就需要使用Response對(duì)象的setContentType(String s);方法來改變contentType屬性的值。

格式:response.setContentType(String s) -----參數(shù)s可取:text/html,application/x-msexcel,application/msword等

Response重定向

在某些情況下,當(dāng)響應(yīng)客戶時(shí),需要將客戶重新引導(dǎo)到另一個(gè)頁面,可以使用Response的sendRedirect(URL);方法,實(shí)現(xiàn)客戶的重定向

格式:response.sendRedirect("index.jsp");

3. Session對(duì)象: 在頁面的page指令加上session="true" 或缺省 情況下以及在servlet中使用request.getSession();的方式進(jìn)行創(chuàng)建。超時(shí)或服務(wù)停止時(shí),session結(jié)束。(一個(gè)用戶對(duì)應(yīng)一個(gè)session對(duì)象)

什么是session對(duì)象? ----- 它是一個(gè)jsp內(nèi)置對(duì)象,它在第一個(gè)jsp被裝載時(shí)自動(dòng)創(chuàng)建,完成會(huì)話期管理。從一個(gè)客戶打開瀏覽器并連接到服務(wù)器開始,到客戶關(guān)閉瀏覽器離 開這個(gè)服務(wù)器結(jié)束(或者超時(shí)),被稱為一個(gè)會(huì)話。當(dāng)一個(gè)客戶訪問一個(gè)服務(wù)器時(shí),可能會(huì)在這個(gè)服務(wù)器的幾個(gè)頁面之間切換,服務(wù)器應(yīng)當(dāng)通過某種辦法知道這是一個(gè)客戶,就需要?jiǎng)?chuàng)建session對(duì)象。

session對(duì)象的ID -----當(dāng)一個(gè)客戶首次訪問服務(wù)器上的一個(gè)jsp頁面時(shí),jsp引擎會(huì)產(chǎn)生一個(gè)session對(duì)象,同時(shí)分配一個(gè)String類型的的ID號(hào),jsp引擎同時(shí)將這個(gè)ID號(hào)發(fā)送到客戶端,存放在cookie中,這樣,session對(duì)象直到客戶關(guān)閉瀏覽器后,服務(wù)器端,該客戶的session對(duì)象才會(huì)消失,會(huì)并,和客戶的會(huì)話對(duì)應(yīng)關(guān)系消失。當(dāng)客戶重新打開瀏覽器再連接到該服務(wù)器時(shí),服務(wù)器為該客戶再創(chuàng)建一個(gè)新的session對(duì)象。

session對(duì)象的常用方法:

public String getId() ----獲取Session對(duì)象的編號(hào)

pulbic void setAttribute(String key,Object obj) --- 將參數(shù)object指定的對(duì)象obj添加到Session對(duì)象中,并為添加的對(duì)象指定一個(gè)索引關(guān)鍵字

public Object getAttribute(String key) --- 獲取session對(duì)象中含有關(guān)鍵字的對(duì)象

public Blooean isNew() ----判斷是否為一個(gè)新的客戶

4. Application對(duì)象:啟動(dòng)服務(wù)時(shí)創(chuàng)建,停止服務(wù)時(shí)銷毀。(整個(gè)程序只有一個(gè)application對(duì)象)

什么是Application對(duì)象? --- 服務(wù)器啟動(dòng)后就產(chǎn)生了這個(gè)Application對(duì)象,當(dāng)客戶在所訪問的的網(wǎng)站的各個(gè)頁面之間瀏覽時(shí),這個(gè)Application對(duì)象都是同一個(gè),直到服務(wù)器關(guān)閉。但是,與session對(duì)象不同的是,所有客戶的Application對(duì)象都是同一個(gè),即,所有客戶共享這個(gè)內(nèi)置的Application對(duì)象。

Application對(duì)象的常用方法

setAttribute(String key,Ojbect obj) ----將參數(shù)obj添加到Application對(duì)象中,并為添加的對(duì)象指定一個(gè)索引關(guān)鍵字

getAttribute(String key); ---- 獲取Application對(duì)象中含有關(guān)鍵字的對(duì)象

5.Out對(duì)象:請(qǐng)求時(shí),就創(chuàng)建,請(qǐng)求結(jié)束時(shí),銷毀。(一個(gè)請(qǐng)求,創(chuàng)建一個(gè)out對(duì)象)

Out對(duì)象是一個(gè)輸出流,用來向客戶端輸出數(shù)據(jù)。out對(duì)象用于各種數(shù)據(jù)的輸出。其常用方法如下:

out.print(); ---- 向?yàn)g覽器輸出各種類型數(shù)據(jù)

out.newLine(); --- 輸出一個(gè)換行符

out.close(); ---關(guān)閉流

6.cookie對(duì)象:

什么是cookie?----cookie是web服務(wù)器保存在用戶硬盤上的一段文本。cookie允許一個(gè)web站點(diǎn)在用戶電腦上保存信息并且隨后再取它。

舉例來說,一個(gè)web站點(diǎn)可能會(huì)為每一個(gè)訪問者產(chǎn)生一個(gè)唯一的ID、然后以cookie文件的形式保存在每個(gè)用戶的機(jī)器上。

如果用戶選擇的是IE瀏覽器訪問web,用戶就會(huì)看到所有保存在自己硬盤上的cookie。它們最常存放的地方是:c:/Windows/Cookies。cookie是以”關(guān)鍵字 key = 值 value“的格式來保存記錄的。

創(chuàng)建一個(gè)cookie對(duì)象

調(diào)用cookid對(duì)象的構(gòu)造函數(shù)就可以創(chuàng)建cookie對(duì)象。cookie的構(gòu)造函數(shù)有兩個(gè)字符串參數(shù):cookie名字和cookie值。

如:Cookie c = new Cookie("username","hyc");

將cookie對(duì)象傳送到客戶端

在jsp中,如果要將封裝好的cookie對(duì)象傳送到客戶端,可使用Response對(duì)象的addCookie()方法

如:Response.addCookie();

讀取保存在客戶端的cookie

使用request對(duì)象的getCookie()方法,執(zhí)行時(shí),將所有客戶端傳來的cookie對(duì)象以數(shù)組的形式排列。如果要取出更符合需求的cookie對(duì)象,就需要循環(huán)比較數(shù)組內(nèi)每個(gè)對(duì)象的關(guān)鍵字。

如:Cookie [] c = request.getCookies();

if (c! = null){

for (int i = 0;ic.length;i++){

if("username".equals("c,getName))

out.println(c.getValue());

}

}

設(shè)置cookie對(duì)象的有效時(shí)間

調(diào)用cookie對(duì)象的setMaxAge()方法,可以設(shè)置cookie對(duì)象的有效時(shí)間。如

Cookie c = new Cookie("username","hyc");

c.setMaxAge(3600);

cookie應(yīng)用

cookie對(duì)象的典型應(yīng)用是用來統(tǒng)計(jì)網(wǎng)站的訪問人數(shù)。由于代理服務(wù)器、緩存等的使用,唯一能幫助網(wǎng)站精確統(tǒng)計(jì)來訪人數(shù)的方法就是為每個(gè)訪問者建立一個(gè)唯 一的ID。使用cookie,網(wǎng)站可以完成以下工作

測(cè)試定多少人訪問過;

測(cè)定訪問者有多少是新用戶(即第一次來訪)、有多少老用戶;

測(cè)定一個(gè)用戶多久訪問一次網(wǎng)站

當(dāng)一個(gè)用戶第一次訪問時(shí),網(wǎng)站在數(shù)據(jù)庫中建立一個(gè)新的ID,并把ID通過Cookie傳送給用戶。用戶再次來訪時(shí),網(wǎng)站把該用戶的ID對(duì)應(yīng)的計(jì)數(shù)器加1,得到用戶來訪的次數(shù)。

7.config對(duì)象:配置對(duì)象

8.page對(duì)象:頁面對(duì)象

9.Exception對(duì)象:在處理異常的網(wǎng)頁中可以直接訪問exception隱式對(duì)象

如何指定 PHP 數(shù)據(jù)類型

php 函數(shù)的參數(shù)類型可以指定為類名或數(shù)組類型array,比如 這樣是對(duì)的public function Right( My_Class $a, array $b ) 這樣是錯(cuò)的public function Wrong( string $a, boolean $b ) 如果需要其他類型

php 連接數(shù)據(jù)庫類

我也剛剛學(xué)PHP,正在研究中,雖然你只給10分........

首先,將代碼保存到一個(gè)文件,如:mysql.class.php

其次,在一個(gè)常用的文件里調(diào)用:比如頭部文件header.php,因?yàn)槲曳旁诟夸浰杂孟旅娣绞綄?dǎo)入其他文件:

require dirname(__FILE__) . 'include/config.php';

//導(dǎo)入類文件

require dirname(__FILE__) . 'include/mysql.class.php';

//定義一個(gè)類及初始化數(shù)據(jù)庫類

$db = new mysql($db_host, $db_user, $db_pass, $db_name);

$db_host = $db_user = $db_pass = $db_name = NULL;

然后,在test.php文件調(diào)用:

require_once dirname(__FILE__) . '/header.php';

使用方法:

$sql = "讀取表";

$res = $db-query($sql);

$info = array();//定義數(shù)組

while($row=$db-fetchRow($res))

{

$arr['id'] = $row['id'];

$arr['title'] = $row['title'];

$info[] = $arr;

}

可以在顯示的地方用:

foreach($info as $i)

{

echo $i['title']."br /";

}

或是直接使用while

還用另一種調(diào)用方式:

$here_area = $db-getRow("select areaid,areaname from {$table}area where areaid='$areaid'");

$here[] = array('name'=$here_area['areaname'],'id'=$here_area['areaid']);

測(cè)試通過,因?yàn)槲艺谑褂?....................................

config.php代碼:

?php

$db_host = "localhost";

$db_name = "test";

$db_user = "root";

$db_pass = "";

$table = "mini_";

$charset = "gb2312";

$dbcharset = "gbk";

?

mysql.class.php代碼:

?php

class mysql

{

var $link = NULL;

//自動(dòng)執(zhí)行__construct php5類構(gòu)建方法,如果PHP4和PHP5同時(shí)使用會(huì)自動(dòng)使用PHP5的方法

function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)

{

//自動(dòng)執(zhí)行時(shí)調(diào)用mysql函數(shù)

$this-mysql($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);

}

//php4類構(gòu)建方法,如果沒有 __construct 就自動(dòng)執(zhí)行此功能

function mysql($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)

{

if ($quiet)

{

$this-connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);

}

else

{

$this-settings = array(

'dbhost' = $dbhost,

'dbuser' = $dbuser,

'dbpw' = $dbpw,

'dbname' = $dbname,

'charset' = $charset,

'pconnect' = $pconnect

);

}

}

function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)

{

global $dbcharset;

if ($pconnect)

{

if (!($this-link = @mysql_pconnect($dbhost, $dbuser, $dbpw)))

{

if (!$quiet)

{

$this-ErrorMsg("Can't pConnect MySQL Server($dbhost)!");

}

return false;

}

}

else

{

if (PHP_VERSION = '4.2')

{

$this-link = @mysql_connect($dbhost, $dbuser, $dbpw, true);

}

else

{

$this-link = @mysql_connect($dbhost, $dbuser, $dbpw);

mt_srand((double)microtime() * 1000000);

}

if (!$this-link)

{

if (!$quiet)

{

$this-ErrorMsg("Can't Connect MySQL Server($dbhost)!");

}

return false;

}

}

$this-dbhash = md5($this-root_path . $dbhost . $dbuser . $dbpw . $dbname);

$this-version = mysql_get_server_info($this-link);

if ($this-version '4.1')

{

if ($dbcharset != 'latin1')

{

mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this-link);

}

if ($this-version '5.0.1')

{

mysql_query("SET sql_mode=''", $this-link);

}

}

if ($dbname)

{

if (mysql_select_db($dbname, $this-link) === false )

{

if (!$quiet)

{

$this-ErrorMsg("Can't select MySQL database($dbname)!");

}

return false;

}

else

{

return true;

}

}

else

{

return true;

}

}

function query($sql, $type = '')

{

if ($this-link === NULL)

{

$this-connect($this-settings['dbhost'], $this-settings['dbuser'], $this-settings['dbpw'], $this-settings['dbname'], $this-settings['charset'], $this-settings['pconnect']);

$this-settings = array();

}

if ($this-queryCount++ = 99)

{

$this-queryLog[] = $sql;

}

if ($this-queryTime == '')

{

if (PHP_VERSION = '5.0.0')

{

$this-queryTime = microtime(true);

}

else

{

$this-queryTime = microtime();

}

}

if (!($query = mysql_query($sql, $this-link)) $type != 'SILENT')

{

$this-error_message[]['message'] = 'MySQL Query Error';

$this-error_message[]['sql'] = $sql;

$this-error_message[]['error'] = mysql_error($this-link);

$this-error_message[]['errno'] = mysql_errno($this-link);

$this-ErrorMsg();

return false;

}

return $query;

}

function affected_rows()

{

return mysql_affected_rows($this-link);

}

function num_fields($query)

{

return mysql_num_fields($query);

}

function error()

{

return mysql_error($this-link);

}

function errno()

{

return mysql_errno($this-link);

}

function num_rows($query)

{

return mysql_num_rows($query);

}

function insert_id()

{

return mysql_insert_id($this-link);

}

function fetchRow($query)

{

return mysql_fetch_assoc($query);

}

function fetcharray($query)

{

return mysql_fetch_array($query);

}

function version()

{

return $this-version;

}

function close()

{

return mysql_close($this-link);

}

function ErrorMsg($message = '', $sql = '')

{

if ($message)

{

echo "$message\n\n";

}

else

{

echo "bMySQL server error report:";

print_r($this-error_message);

}

exit;

}

function getCol($sql)

{

$res = $this-query($sql);

if ($res !== false)

{

$arr = array();

while ($row = mysql_fetch_row($res))

{

$arr[] = $row[0];

}

return $arr;

}

else

{

return false;

}

}

function getOne($sql, $limited = false)

{

if ($limited == true)

{

$sql = trim($sql . ' LIMIT 1');

}

$res = $this-query($sql);

if ($res !== false)

{

$row = mysql_fetch_row($res);

if ($row !== false)

{

return $row[0];

}

else

{

return '';

}

}

else

{

return false;

}

}

function getAll($sql)

{

$res = $this-query($sql);

if ($res !== false)

{

$arr = array();

while ($row = mysql_fetch_assoc($res))

{

$arr[] = $row;

}

return $arr;

}

else

{

return false;

}

}

//使用: getRow($sql,true) 如果有true那值是 limit 1,讀取一條信息

function getRow($sql, $limited = false)

{

if ($limited == true)

{

$sql = trim($sql . ' LIMIT 1');

}

$res = $this-query($sql);

if ($res !== false)

{

return mysql_fetch_assoc($res);

}

else

{

return false;

}

}

}

?

當(dāng)前名稱:php未定義數(shù)據(jù)庫類型 數(shù)據(jù)庫函數(shù)未定義
轉(zhuǎn)載注明:http://muchs.cn/article24/doshdce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站改版App設(shè)計(jì)、靜態(tài)網(wǎng)站營銷型網(wǎng)站建設(shè)、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)