关于mysql_fetch_row(),mysql_fetch_assoc(),mysql_fetch_array(),mysql_fetch_object(),mysql_result()的区别。

<?php
/**mysql的几种查询区别和用法
 **mysql_fetch_row(resource $result); 返回array,结果只有数字索引;
 **mysql_fetch_array(resource $result);返回的array结果中既有数字索引,也有以表的列和值的键值对;
 **mysql_fetch_assoc(resource $result);返回的只有键值对的结果;
 **mysql_feych_object(resource $result);返回键值对但是不是数组,是对象;
 **mysql_result($result,$row,$field="");返回一个字符串,默认返回第一列;
*/
header("Content-Type:text/html;charset:utf-8");
$host="localhost";
$user="root";
$psd="root";


$con=mysql_connect($host,$user,$psd)or die("database link failed");


if(!$con){
echo "fail";
}else{
echo "<pre>";
// echo "success";
mysql_select_db("blog",$con);
$result=mysql_query('select * from test');
####################################################################################
// while($row=mysql_fetch_row($result)){
// print_r($row);
// }


//结果如下:
// Array
// (
//     [0] => 1
//     [1] => hello
//     [2] => the world is so beautiful
// )
// Array
// (
//     [0] => 2
//     [1] => family
//     [2] => the fammily is so warm
// )


#####################################################################################
// while($array=mysql_fetch_array($result)){
// print_r($array);
// }


// 结果如下:
// Array
// (
//     [0] => 1
//     [id] => 1
//     [1] => hello
//     [title] => hello
//     [2] => the world is so beautiful
//     [body] => the world is so beautiful
// )
// Array
// (
//     [0] => 2
//     [id] => 2
//     [1] => family
//     [title] => family
//     [2] => the fammily is so warm
//     [body] => the fammily is so warm
// )


#####################################################################################
// while($assoc=mysql_fetch_assoc($result)){
// print_r($assoc);
// }


//结果为:
// Array
// (
//     [id] => 1
//     [title] => hello
//     [body] => the world is so beautiful
// )
// Array
// (
//     [id] => 2
//     [title] => family
//     [body] => the fammily is so warm
// )


#####################################################################################
// while ($object=mysql_fetch_object($result)) {
// print_r($object);
// }


// 结果为:
// stdClass Object
// (
//     [id] => 1
//     [title] => hello
//     [body] => the world is so beautiful
// )
// stdClass Object
// (
//     [id] => 2
//     [title] => family
//     [body] => the fammily is so warm
// )
$mr=mysql_result($result,0);
echo $mr;
$mr1=mysql_result($result,1);
echo $mr1.'<br>';
$mr_title=mysql_result($result,0,'title');
echo $mr_title;


// 结果为:
// 1
// 2
// hello


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值