数据库提取数据函数分析

自学的时候,碰到数据库提取数据的时候十分困惑,然后查找手册,从手册上总结了一下。当时我用的php大版本是5.5的,自从5.7后已经不再使用这种方法,强烈建议直接使用PDO或者是mysqli类。
<?php 
/**

mysql_fetch_assoc()		取出一行作为关联数组(最常用)
mysql_fetch_array()		取出一行作为关联数组或者数字数字
mysql_fetch_row() 		取出一行作为枚举数组
mysql_fetch_length()	从mysql_fetch_row()取得的行中取得长度
mysql_fetch_filed()		取出列信息作为对象返回
mysql_fetch_object()	取出一行作为对象
**/

$conn = mysql_connect('localhost', 'root', '123456');
$sql = 'use boolshop';
mysql_query($sql, $conn);

$sql = 'set names utf8';
mysql_query($sql, $conn);

$sql = 'select goods_id, goods_name, shop_price, market_price from goods order by shop_price desc limit 5';
$rs = mysql_query($sql,$conn);


$row = mysql_fetch_array($rs);
var_dump($row);
	/*
	array (size=8)
		  0 => string '3' (length=1)
		  'goods_id' => string '3' (length=1)
		  1 => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
		  'goods_name' => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
		  2 => string '1490.00' (length=7)
		  'shop_price' => string '1490.00' (length=7)
		  3 => string '1643.00' (length=7)
		  'market_price' => string '1643.00' (length=7)
	*/

$row = mysql_fetch_assoc($rs);
var_dump($row);exit;
	/*
	array (size=4)
		  'goods_id' => string '3' (length=1)
		  'goods_name' => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
		  'shop_price' => string '1490.00' (length=7)
		  'market_price' => string '1643.00' (length=7)
	*/

$row = mysql_fetch_row($rs);
var_dump($row);
	/*
		array (size=4)
		  0 => string '3' (length=1)
		  1 => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
		  2 => string '1490.00' (length=7)
		  3 => string '1643.00' (length=7)
	*/

$row = mysql_fetch_field($rs);
var_dump($row);
	/*
	object(stdClass)[1]
		  public 'name' => string 'goods_id' (length=8)
		  public 'table' => string 'goods' (length=5)
		  public 'def' => string '' (length=0)
		  public 'max_length' => int 2
		  public 'not_null' => int 1
		  public 'primary_key' => int 1
		  public 'multiple_key' => int 0
		  public 'unique_key' => int 0
		  public 'numeric' => int 1
		  public 'blob' => int 0
		  public 'type' => string 'int' (length=3)
		  public 'unsigned' => int 1
		  public 'zerofill' => int 0
	*/

$row = mysql_fetch_object($rs);
var_dump($row);exit;
	/*
	object(stdClass)[1]
		  public 'goods_id' => string '3' (length=1)
		  public 'goods_name' => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
		  public 'shop_price' => string '1490.00' (length=7)
		  public 'market_price' => string '1643.00' (length=7)
	*/



$conn = mysql_connect('localhost', 'root', '123456');
$sql = 'use boolshop';
mysql_query($sql, $conn);


$sql = 'set names utf8';
mysql_query($sql, $conn);


$sql = 'select goods_id, goods_name, shop_price, market_price from goods order by shop_price desc limit 5';
$rs = mysql_query($sql,$conn);




$row = mysql_fetch_array($rs);
var_dump($row);
/*
array (size=8)
 0 => string '3' (length=1)
 'goods_id' => string '3' (length=1)
 1 => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
 'goods_name' => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
 2 => string '1490.00' (length=7)
 'shop_price' => string '1490.00' (length=7)
 3 => string '1643.00' (length=7)
 'market_price' => string '1643.00' (length=7)
*/


$row = mysql_fetch_assoc($rs);
var_dump($row);exit;
/*
array (size=4)
 'goods_id' => string '3' (length=1)
 'goods_name' => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
 'shop_price' => string '1490.00' (length=7)
 'market_price' => string '1643.00' (length=7)
*/


$row = mysql_fetch_row($rs);
var_dump($row);
/*
array (size=4)
 0 => string '3' (length=1)
 1 => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
 2 => string '1490.00' (length=7)
 3 => string '1643.00' (length=7)
*/


$row = mysql_fetch_field($rs);
var_dump($row);
/*
object(stdClass)[1]
 public 'name' => string 'goods_id' (length=8)
 public 'table' => string 'goods' (length=5)
 public 'def' => string '' (length=0)
 public 'max_length' => int 2
 public 'not_null' => int 1
 public 'primary_key' => int 1
 public 'multiple_key' => int 0
 public 'unique_key' => int 0
 public 'numeric' => int 1
 public 'blob' => int 0
 public 'type' => string 'int' (length=3)
 public 'unsigned' => int 1
 public 'zerofill' => int 0
*/


$row = mysql_fetch_object($rs);
var_dump($row);exit;
/*
object(stdClass)[1]
 public 'goods_id' => string '3' (length=1)
 public 'goods_name' => string '两扣平驳领棕色格纹男士休闲单西D6959' (length=50)
 public 'shop_price' => string '1490.00' (length=7)
 public 'market_price' => string '1643.00' (length=7)
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值