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