新聞中心
php怎么獲取數(shù)據(jù)庫查詢返回的結(jié)果
從查詢結(jié)果取值,需要遍歷結(jié)果集!示例如下:
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、恩陽ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的恩陽網(wǎng)站制作公司
$rs?=?mysql_query("select?*?from?www_liu?where?xx='$xx'?and?yy='$yy'");
echo?"查詢信息如下:br/";
while($row?=?mysql_fetch_array($rs))
{
echo?$row['字段2']?.?"====="?.?$row['字段三'];
echo?"br?/";
}
//關(guān)閉數(shù)據(jù)庫連接
//mysql_close();
php 取得結(jié)果集中行的數(shù)目 有幾種方法
主要2種:
獲取查詢結(jié)果的記錄數(shù)
int mysql_num_rows(mysql_result $result)
$result 為 mysql_query 返回的結(jié)果集。
[該函數(shù)返回一個(gè)整數(shù),表示記錄中有多少行數(shù)據(jù)]
mysql_affected_row()
可以用來獲取 insert, update, delete語句影響的記錄行數(shù)。
例:
--------------------------------------------
$sql = "select * from course";
$rs = mysql_query($sql) or die('數(shù)據(jù)庫連接失敗');
$s = mysql_num_rows($rs); //獲取記錄數(shù)
--------------------------------------------
PHP怎么獲取表單提交的數(shù)據(jù)啊?
一、用file_get_contents以get方式獲取內(nèi)容,需要輸入內(nèi)容為:
1、?php
2、$url='';
3、$html=file_get_contents($url);
4、echo$html;
5、?
二、用file_get_contents函數(shù),以post方式獲取url,需要輸入內(nèi)容為
1、?php
2、$url='';
3、$data=array('foo'='bar');
4、$data=http_build_query($data);
5、$opts=array(
6、'http'=array(
7、?'method'='POST',
8、?'header'="Content-type:application/x-www-form-urlencoded\r\n".
9、??????????"Content-Length:".strlen($data)."\r\n",
10、?'content'=$data
11、)
12、);
13、$ctx=stream_context_create($opts);
14、$html=@file_get_contents($url,'',$ctx);
15、?
三、用fopen打開url,以get方式獲取內(nèi)容,需要輸入內(nèi)容為
1、?php
2、$fp=fopen($url,'r');
3、$header=stream_get_meta_data($fp);//獲取信息
4、while(!feof($fp)){
5、$result.=fgets($fp,1024);
6、}
7、echo"urlheader:{$header}br":
8、echo"urlbody:$result";
9、fclose($fp);
10、?
四、用fopen打開url,以post方式獲取內(nèi)容,需要輸入內(nèi)容為
1、?php
2、$data=array('foo2'='bar2','foo3'='bar3');
3、$data=http_build_query($data);
4、$opts=array(
5、'http'=array(
6、'method'='POST',
7、'header'="Content-type:application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n".
8、"Content-Length:".strlen($data)."\r\n",
9、'content'=$data
10、)
11、);
12、$context=stream_context_create($opts);
13、$html=fopen(';id2=i4','rb',false,$context);
14、$w=fread($html,1024);
15、echo$w;
16、?
五、用fsockopen函數(shù)打開url,以get方式獲取完整的數(shù)據(jù),包括header和body,需要輸入內(nèi)容為
1、?php
2、functionget_url($url,$cookie=false)
3、{
4、$url=parse_url($url);
5、$query=$url[path]."?".$url[query];
6、echo"Query:".$query;
7、$fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
8、if(!$fp){
9、returnfalse;
10、}else{
11、$request="GET$queryHTTP/1.1\r\n";
12、$request.="Host:$url[host]\r\n";
13、$request.="Connection:Close\r\n";
14、if($cookie)$request.="Cookie:??$cookie\n";
15、$request.="\r\n";
16、fwrite($fp,$request);
17、while(!@feof($fp)){
18、$result.=@fgets($fp,1024);
19、}
20、fclose($fp);
21、return$result;
22、}
23、}
24、//獲取url的html部分,去掉header
25、functionGetUrlHTML($url,$cookie=false)
26、{
27、$rowdata=get_url($url,$cookie);
28、if($rowdata)
29、{
30、$body=stristr($rowdata,"\r\n\r\n");
31、$body=substr($body,4,strlen($body));
32、return$body;
33、}
34、?returnfalse;
35、}
36、?
參考資料:
php-file_get_contents
程序員 PHP PHP和MySQL查詢的結(jié)果集
?php
$conn=mysql_connect("localhost","root","");
$select=mysql_select_db("books",$conn);
$query="insert into computers(name,price,publish_data) ";
$query.="values('JSP',28.00,'2008-11-1')";
$query="select * from computers";
$result=mysql_query($query);
//以下是使用mysql_result()函數(shù)來獲取到查詢結(jié)果
$num=mysql_num_rows($result);
for($rows_count=0;$rows_count$num;$rows_count++){
echo "書名:".mysql_result($result,$rows_count,"name");
echo "價(jià)格:".mysql_result($result,$rows_count,"price");
echo "出版日期:".mysql_result($result,$rows_count,"publish_data")."br";
}
//以下是使用mysql_fetch_row()函數(shù)來獲取到查詢結(jié)果
while($row=mysql_fetch_row($result))
{
echo "書號(hào):".$row[0]."br";
echo "書名:".$row[1]."br";
echo "價(jià)格:".$row[2]."br";
echo "出版日期:".$row[3]."br";
echo "br";
}
//以下是使用mysql_fetch_array()函數(shù)來獲取到查詢結(jié)果
while($row=mysql_fetch_array($result))
{
echo "書號(hào):".$row[0]."br";
echo "書名:".$row[1]."br";
echo "價(jià)格:".$row["price"]."br";
echo "出版日期:".$row["publish_data"]."br";
echo "br";
}
//mysql_fetch_assoc()同mysql_fetch_array($result,MYSQL_ASSOC)一樣
while($row = mysql_fetch_assoc($res)){
echo $row['price'].'::'.$row['publish_data'].”;
} //$row[0]不能取值
//以下是使用mysql_fetch_object()函數(shù)來獲取到查詢結(jié)果
while($row=mysql_fetch_object($result))
{
echo "書號(hào):".$row-id."br";
echo "書名:".$row-name."br";
echo "價(jià)格:".$row-price."br";
echo "出版日期:".$row-publish_data."br";
echo "br";
}
?
綜合比較
本節(jié)主要介紹了獲取查詢結(jié)果集的4個(gè)函數(shù),此處對它們進(jìn)行綜合比較。
● mysql_result():優(yōu)點(diǎn)在于使用方便;而缺點(diǎn)在于功能少,一次調(diào)用只能獲取結(jié)果數(shù)據(jù)集中的一行記錄,對較大型的數(shù)據(jù)庫效率較低。
● mysql_fetch_row():優(yōu)點(diǎn)在于執(zhí)行效率在4種方法中最高;不足在于只能用數(shù)字作為屬性索引來獲得屬性值,在使用時(shí)非常容易出現(xiàn)混淆。
● mysql_fetch_array():執(zhí)行效率同樣很高,同mysql_fetch_row()相差無幾,并且可以用屬性名方式直接獲取得屬性值,因此,在實(shí)際應(yīng)用中最常用。
● mysql_fetch_object():采用了面向?qū)ο蟮乃枷耄谠O(shè)計(jì)思路上更為先進(jìn),如果讀者習(xí)慣于面向?qū)ο蟮乃悸穪韺懗绦颍瑒t會(huì)很自然的選擇它。其次,該方法的優(yōu)點(diǎn)還體現(xiàn)在,對于結(jié)構(gòu)較為復(fù)雜的數(shù)據(jù)結(jié)果,在邏輯上顯得更為清晰。
后3個(gè)函數(shù)的共同點(diǎn)在于,都是取得當(dāng)前行的數(shù)據(jù),然后自動(dòng)滑向后一行。有時(shí)候,希望控制滑動(dòng)的行數(shù),這是常常搭配使用的一個(gè)函數(shù)是mysql_data_seek(),其定義為:
int mysql_data_seek(int result_identifier,int row_number)
調(diào)用該函數(shù)可以在結(jié)果集中向后滑動(dòng)row_number行,在下一次調(diào)用mysql_fetch_*函數(shù)時(shí),讀取的將是向后滑動(dòng)row_number行后的記錄。
網(wǎng)站名稱:php獲取數(shù)據(jù)結(jié)果集 php獲取參數(shù)值的三種方式
文章分享:http://ef60e0e.cn/article/ddjisog.html