首页 > 编程技术 > php

php中返回查询数据集四种方法

发布时间:2016-11-25 17:11

php中 mysql_result mysql_fetch_row mysql_fetch_array mysql_fetch_object四种函数的详细说明

mysql教程_result():优点在于使用方便;其缺点在于功能少,一次调用只能获取结果数据集中的一行元素,对较大型的数据库教程效率较低;

mysql_result() 函数返回结果集中一个字段的值。

如果成功,则该函数返回字段值。如果失败,则返回 false。

语法
mysql_result(data,row,field)参数 描述
data 必需。规定要使用的结果标识符。该标识符是 mysql_query() 函数返回的。
row 必需。规定行号。行号从 0 开始。
field 可选。规定获取哪个字段。可以是字段偏移值,字段名或 table.fieldname。

如果该参数未规定,则该函数从指定的行获取第一个字段。
 

<?php教程
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
  {
  die('could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db", $con);

$sql = "select * from person";
$result = mysql_query($sql,$con);

echo mysql_result($result,0);

mysql_close($con);

?>

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

mysql_fetch_row() 函数从结果集中取得一行作为数字数组。

语法
mysql_fetch_row(data)参数 描述
data 必需。要使用的数据指针。该数据指针是从 mysql_query() 返回的结果。

说明
mysql_fetch_row() 从和结果标识 data 关联的结果集中取得一行数据并作为数组返回。每个结果的列储存在一个数组的单元中,偏移量从 0 开始。

依次调用 mysql_fetch_row() 将返回结果集中的下一行,如果没有更多行则返回 false。

返回值
返回根据所取得的行生成的数组,如果没有更多行则返回 false。
例子

<?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
  {
  die('could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);
$sql = "select * from person where lastname='adams'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_row($result));

mysql_close($con);
?>输出:

array
(
[0] => adams
[1] => john
[2] => london
)


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

定义和用法
mysql_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有

返回根据从结果集取得的行生成的数组,如果没有更多行则返回 false。

语法
mysql_fetch_array(data,array_type)参数 描述
data 可选。规定规定要使用的数据指针。该数据指针是 mysql_query() 函数产生的结果。
array_type 可选。规定返回哪种结果。可能的值:

mysql_assoc - 关联数组
mysql_num - 数字数组
mysql_both - 默认。同时产生关联和数字数组
 
提示和注释
注释:mysql_fetch_array() 是 mysql_fetch_row() 的扩展版本。除了将数据以数字索引方式储存在数组中之外,还可以将数据作为关联索引储存,用字段名作为键名。

提示:有很重要的一点必须指出,用 mysql_fetch_array() 并不明显比用 mysql_fetch_row() 慢,而且还明显提供了更多的值。

注释:本函数返回的字段名是区分大小写的。
例子

<?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
  {
  die('could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);
$sql = "select * from person where lastname='adams'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_array($result));

mysql_close($con);
?>输出类似:

array
(
[0] => adams
[lastname] => adams
[1] => john
[firstname] => john
[2] => london
[city] => london
)

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

mysql_fetch_object() 函数从结果集(记录集)中取得一行作为对象。

若成功的话,本函数从 mysql_query() 获得一行,并返回一个对象。如果失败或没有更多的行,则返回 false。

语法
mysql_fetch_object(data)参数 描述
data 必需。要使用的数据指针。该数据指针是从 mysql_query() 返回的结果。
提示和注释
注释:每个随后对 mysql_fetch_object() 的调用都会返回记录集中的下一行。

注释:mysql_fetch_object() 与 mysql_fetch_array() 类似,只有一点区别 - 返回的是对象而不是数组。间接地,也意味着只能通过字段名来访问数组,而不是偏移量。
例子

<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
  {
  die('could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);
$sql = "select * from person";
$result = mysql_query($sql,$con);

while ($row = mysql_fetch_object($result))
  {
  echo $row->firstname . "<br />";
  }

mysql_close($con);
?>输出:

john
george
thomas

php教程 ereg()邮箱验证与文件名验证
字符串比对解析。

语法: int ereg(string pattern, string string, array [regs]);

返回值: 整数/数组

函数种类: 资料处理


 
 
内容说明


本函数以 pattern 的规则来解析比对字符串 string。比对结果返回的值放在数组参数 regs 之中,regs[0] 内容就是原字符串 string、regs[1] 为第一个合乎规则的字符串、regs[2] 就是第二个合乎规则的字符串,余类推。若省略参数 regs,则只是单纯地比对,找到则返回值为 true。
 
 
使用范例
这个例子是 markus@dnet.it 在 14-jun-1999 所提出的,可对使用者输入的 e-mail 作简单的检查,检查使用者的 e-mail 字符串是否有 @ 字符,在 @ 字符前有英文字母或数字,在之后有数节字符串,最后的小数点后只能有二个或三个英文字母。super@mail.wilson.gs 就可以通过检查,super@mail.wilson 就不能通过检查。

<?php
if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email)) {
  echo "您的 e-mail 通过初步检查";
}
?>

看一下

dedecms文件验证

if(!eregi("^[a-z0-9_-]{1,}.lib.php$",$filename))
  {
   showmsg('文件不是标准的标签碎片文件,不允许在此编辑!','-1');
   exit();
  }
  $fp = fopen(dedeinc.'/taglib/'.$filename,'r');
  $democode = fread($fp,filesize(dedeinc.'/taglib/'.$filename));
  fclose($fp);
  $title = "修改标签";

 

取得 php教程 的配置选项值。

语法: string get_cfg_var(string varname);

返回值: 字符串

函数种类: php 系统功能
内容说明

若正确取得目前 php 配置选项 varname,则返回变量值。失败则返回 false。

 

*/
echo "php设置的最大执行时间为:n";
print_r(get_cfg_var("max_execution_time"));       //获得配置项的值
echo "<br>n";
echo "php设置的每个脚本的可使用内存总量为:n";
print_r(get_cfg_var("memory_limit"));         //获得配置项的值
echo "<br>n";
echo "php设置的接受post的最大大小为:n";
print_r(get_cfg_var("post_max_size"));        //获得配置项的值
echo "<br>n";
echo "php设置smtp服务器名为:n";
print_r(get_cfg_var("smtp"));          //获得配置项的值
echo "<br>n";
echo "php设置的上传文件的最大大小为:n";
print_r(get_cfg_var("upload_max_filesize"));       //获得配置项的值
echo "<br>n";

php教程 图形处理函数imagetypes() imagecreatetruecolor() imagecreate()
//判断当前的gd库是否支持png

if(imagetypes() & img_png)
{
  echo "png support is enabled";
}
else
{
  echo "png support is disabled";
}
/*
int imagetypes ( void )


本函数以比特字段方式返回与当前 php 版本关联的 gd 库所支持的图像格式。将返回以下结果,img_gif | img_jpg | img_png | img_wbmp| img_xpm。 例如要检查是否支持 png

*/

//创建图像
$img=imagecreatetruecolor(300,200);
//取得图像宽度
echo imagesx($img);


/*
看个实例
*/

//建立一幅 100x30 的图像
$im=imagecreate(100,30);
//白色背景和蓝色文本
$bg=imagecolorallocate($im,255,255,255);
$textcolor=imagecolorallocate($im,0,0,255);
//把字符串写在图像左上角
imagestring($im,5,0,0,"hello world!",$textcolor);
//输出图像
header("content-type: image/png");
imagepng($im);


/*
如果你想创建一个png图像*透明*,其中的背景是完全透明的,所有行动发生在借鉴,除此之外,然后执行下列操作:
*/

$png = imagecreatetruecolor(800, 600);
    imagesavealpha($png, true);
    $trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
    imagefill($png, 0, 0, $trans_colour);
   
    $red = imagecolorallocate($png, 255, 0, 0);
    imagefilledellips教程教程e($png, 400, 300, 400, 300, $red);
   
    header("content-type: image/png");
    imagepng($png);

<?php教程


//连接数据库教程
$link = mysql教程_connect('localhost','root','密码') or die(mysql_error());
mysql_select_db('sortclass',$link);
mysql_query("set names 'gbk'");
//无限分类类库
class sortclass{

var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $link;
var $table;
function sortclass($link, $table){
$this->setnode(0, -1, '顶极节点');
$this->link = $link;
$this->table = $table;
$node = array();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setnode($node['cid'],$node['pid'],$node['cname']);
}
}
function setnode ($id, $parent, $value){
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
}
function getlist (&$tree, $root= 0){
foreach ($this->child[$root] as $key=>$id){
$tree[] = $id;
if ($this->child[$id]) $this->getlist($tree, $id);
}
}
function getvalue ($id){return $this->data[$id];}
function getlayer ($id, $space = false){
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
}
function getparent ($id){return $this->parent[$id];}
function getparents ($id){
while ($this->parent[$id] != -1){
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
}
function getchild ($id){return $this->child[$id];}
function getchilds ($id = 0){
$child = array($id);
$this->getlist($child, $id);
return $child;
}
function addnode($name,$pid){
mysql_query("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
}
function modnode($cid, $newname){
mysql_query("update $this->table set `cname`='$newname' where `cid` = $cid",$this->link);
}
function delnode($cid){
$allchilds = $this->getchilds($cid);
$sql ='';
if(empty($allchilds)){
$sql = "delete from $this->table where `cid` = $cid";
}else{
$sql = 'delete from '.$this->table.' where `cid` in ('.implode(',',$allchilds).','.$cid.')';
}
mysql_query($sql,$this->link);
}
function movenode($cid, $topid){
mysql_query("update $this->table set `pid`=$topid where `cid` = $cid", $this->link);
}
}
//函数
function back(){
echo '<script language="网页特效">window.location.href="class.php?"+new date().gettime();</script>';
exit;
}
//生成select
function makeselect($array,$formname){
global $tree;
$select = '<select name="'.$formname.'">';
foreach ($array as $id){
$select.='<option value="'.$id.'">'.$tree->getlayer($id, '|-').$tree->getvalue($id)."</option>";
}
return $select.'</select>';
}
$tree = new sortclass($link,'`class`');
$op = !empty($_post['op']) ? $_post['op'] : $_get['op'];
if(!empty($op)){

if($op=='add'){
$tree->addnode($_post['cname'],$_post['pid']);
back();
}

if($op=='mod'){
$tree->modnode($_post['cid'],$_post['cname']);
back();
}

if($op=='del'){
$tree->delnode($_get['cid']);
back();
}

if($op=='move'){
$tree->movenode($_post['who'],$_post['to']);
back();
}
}
$category = $tree->getchilds();
?>
<style type="text/css教程">
body{font-size:12px;}
ul{list-style:none;}
a{cursor:pointer;}
input{ margin-top:8px;}
</style>
<script language="javascript">
function $(e){return document.getelementbyid(e);}
function mod(cid){
$('cid').value=cid;
$('op').value='mod';
$('name').style.border='1px solid red';
}
</script>
<h3>添加分类</h3>
<form action="class.php" method="post">
名称:<input type="text" id="name" name="cname" /> 添加到:<?=makeselect($category,'pid')?><br />
<input type="hidden" id="op" name="op" value="add" />
<input type="hidden" id="cid" name="cid" />
<input type="submit" value="添加分类" />
</form>
<h3>移动分类</h3>
<form action="class.php" method="post">
<?=makeselect($category,'who')?>移动到:<?=makeselect($category,'to')?>
<input type="hidden" id="op" name="op" value="move" />
<input type="submit" value="移动" />
</form>
<ul>
<?php
foreach ($category as $id){
echo '<li>'.$tree->getlayer($id, '|- ').$tree->getvalue($id).' <a href="class.php?op=del&cid='.$id.'">del</a> <a onclick="mod('.$id.')">edit</a> </li>';
}
?>
</ul>
完整的新闻无限级分类代码,可添加,删除,移动,修改


顶极节点 del   edit
|- 1级分类del   edit
|- 1级分类 del   edit
|- 1级分类 del   edit
|- |- 2级分类 del   edit
|- |- |- 3级分类 del  edit
|- |- |-|- 4级分类 del  edit
|- 1级分类 del   edit


<!--

create database `sortclass`default charset utf8;
create table if not exists `class` (
`cid` mediumint(8) unsigned not null auto_increment,
`pid` mediumint(8) unsigned not null,
`cname` varchar(50) not null,
primary key (`cid`),
key `pid` (`pid`)
) engine=myisam default charset=utf8;

--->

标签:[!--infotagslink--]

您可能感兴趣的文章: