<?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
}
/**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
}