php获取数据结果集 php获取参数值的三种方式

php怎么获取数据库查询返回的结果

从查询结果取值,需要遍历结果集!示例如下:

$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 /”;

  }

//关闭数据库连接

//mysql_close();

php 取得结果集中行的数目 有几种方法

主要2种:

获取查询结果的记录数

int mysql_num_rows(mysql_result $result)

$result 为 mysql_query 返回的结果集。

[该函数返回一个整数,表示记录中有多少行数据]

mysql_affected_row()

可以用来获取 insert, update, delete语句影响的记录行数。

例:

——————————————–

$sql = “select * from course”;

$rs = mysql_query($sql) or die(‘数据库连接失败’);

$s = mysql_num_rows($rs); //获取记录数

——————————————–

PHP怎么获取表单提交的数据啊?

一、用file_get_contents以get方式获取内容,需要输入内容为:

1、?php

2、$url=”;

3、$html=file_get_contents($url);

4、echo$html;

5、?

二、用file_get_contents函数,以post方式获取url,需要输入内容为

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);

[email protected]/* =128)o=(parseInt(m)1)break; e+=’%’+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */_get_contents($url,”,$ctx);

15、?

三、用fopen打开url,以get方式获取内容,需要输入内容为

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方式获取内容,需要输入内容为

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函数打开url,以get方式获取完整的数据,包括header和body,需要输入内容为

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([email protected]/* =128)o=(parseInt(m)1)break; e+=’%’+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */($fp)){

[email protected]/* =128)o=(parseInt(m)1)break; e+=’%’+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */($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查询的结果集

?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()函数来获取到查询结果

$num=mysql_num_rows($result);

for($rows_count=0; $rows_count$num; $rows_count++){

echo “书名:”.mysql_result($result,$rows_count,”name”);

echo “价格:”.mysql_result($result,$rows_count,”price”);

echo “出版日期:”.mysql_result($result,$rows_count,”publish_data”).”br”;

}

//以下是使用mysql_fetch_row()函数来获取到查询结果

while($row=mysql_fetch_row($result))

{

echo “书号:”.$row[0].”br”;

echo “书名:”.$row[1].”br”;

echo “价格:”.$row[2].”br”;

echo “出版日期:”.$row[3].”br”;

echo “br”;

}

//以下是使用mysql_fetch_array()函数来获取到查询结果

while($row=mysql_fetch_array($result))

{

echo “书号:”.$row[0].”br”;

echo “书名:”.$row[1].”br”;

echo “价格:”.$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()函数来获取到查询结果

while($row=mysql_fetch_object($result))

{

echo “书号:”.$row-id.”br”;

echo “书名:”.$row-name.”br”;

echo “价格:”.$row-price.”br”;

echo “出版日期:”.$row-publish_data.”br”;

echo “br”;

}

?

综合比较

本节主要介绍了获取查询结果集的4个函数,此处对它们进行综合比较。

● mysql_result():优点在于使用方便;而缺点在于功能少,一次调用只能获取结果数据集中的一行记录,对较大型的数据库效率较低。

● mysql_fetch_row():优点在于执行效率在4种方法中最高;不足在于只能用数字作为属性索引来获得属性值,在使用时非常容易出现混淆。

● mysql_fetch_array():执行效率同样很高,同mysql_fetch_row()相差无几,并且可以用属性名方式直接获取得属性值,因此,在实际应用中最常用。

● mysql_fetch_object():采用了面向对象的思想,在设计思路上更为先进,如果读者习惯于面向对象的思路来写程序,则会很自然的选择它。其次,该方法的优点还体现在,对于结构较为复杂的数据结果,在逻辑上显得更为清晰。

后3个函数的共同点在于,都是取得当前行的数据,然后自动滑向后一行。有时候,希望控制滑动的行数,这是常常搭配使用的一个函数是mysql_data_seek(),其定义为:

int mysql_data_seek(int result_identifier,int row_number)

调用该函数可以在结果集中向后滑动row_number行,在下一次调用mysql_fetch_*函数时,读取的将是向后滑动row_number行后的记录。