php查询数据库表数据 php数据库查询系统

thinkphp008. 数据库的数据查询

008. 数据库的数据查询

本节课我们来了解一下数据库的数据查询方式,单数据、数据集和其它查询。

一.单数据查询

1. Db::table()中table必须指定完整数据表(包括前缀);

2. 如果希望只查询一条数据,可以使用find()方法,需指定where条件;

Db::table(‘tp_user’)-where(‘id’, 27)-find()

3. Db::getLastSql()方法,可以得到最近一条SQL查询的原生语句;

SELECT * FROM `tp_user` LIMIT 1

4. 没有查询到任何值,则返回null;

5. 使用findOrFail()方法同样可以查询一条数据,在没有数据时抛出一个异常;

Db::table(‘tp_user’)-where(‘id’, 1)-findOrFail()

6. 使用findOrEmpty()方法也可以查询一条数据,但在没有数据时返回一个空数组;

7. Db::table(‘tp_user’)-where(‘id’, 1)-findOrEmpty();

二.数据集查询

1. 想要获取多列数据,可以使用select()方法;

Db::table(‘tp_user’)-select(); SELECT * FROM `tp_user`

2. 多列数据在查询不到任何数据时返回空数组,使用selectOrFail()抛出异常; Db::table(‘tp_user’)-where(‘id’, 1)-selectOrFail();

3. 在select()方法后再使用toArray()方法,可以将数据集对象转化为数组;

4. 当在数据库配置文件中设置了前缀,那么我们可以使用name()方法忽略前缀; Db::name(‘user’)-select();

三.其它查询

1. 通过value()方法,可以查询指定字段的值(单个),没有数据返回null;

Db::name(‘user’)-where(‘id’, 27)-value(‘username’);

$user = Db::table(‘tp_user’)-select()-toArray(); dump($user);

2. 通过colunm()方法,可以查询指定列的值(多个),没有数据返回空数组; Db::name(‘user’)-column(‘username’);

3. 可以指定id作为列值的索引;

4. 如果处理的数据量巨大,成百上千那种,一次性读取有可能会导致内存开销过大;

5. 为了避免内存处理太多数据出错,可以使用chunk()方法分批处理数据;

6. 比如,每次只处理100条,处理完毕后,再读取100条继续处理;

7. 可以利用游标查询功能,可以大幅度减少海量数据的内存开销,它利用了PHP生成器特性。每次查询只读一行,然后再读取时,自动定位到下一行继续读取;

Db::name(‘user’)-column(‘username’, ‘id’);

Db::table(‘tp_user’)-chunk(3, function($users) { foreach ($users as $user) {

dump($user);

}

echo 1; } );

$cursor = Db::table(‘tp_user’)-cursor(); foreach($cursor as $user){

dump($user);

}

用PHP代码如何查询数据库表中的一条记录

你的意思是说

点击查询后

要吧与关键字相关联的整条记录都显示出来?

那样的话

你要先把这条记录复制

给某个数组,然后输出这个数组就可以了

$sql=”select

*

from

db1

where

name=$_post[name]”;

$result=mysql_query($sql,$con);

$row=mysql_fetch_array($result)

echo

$row[name];

echo

$row[age];

……

php如何查询数据库表中的数据并显示

这个简单啊!

首页做个前台输入姓名和会员卡信息的页面,我做个简单的页面给你看

!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “

html xmlns=”

head

meta http-equiv=”Content-Type” content=”text/html;  charset=utf-8″ /

title会员查询系统/title

/head

body

form id=”form1″ name=”form1″ method=”post” action=”test.php”

  p

    label for=”name”/label

    input type=”text” name=”name” id=”name” /

  /p

  p

    label for=”vipid”/label

    input type=”text” name=”vipid” id=”vipid” /

  /p

  p

    input type=”submit” name=”button” id=”button” value=”查询” /

  /p

/form

/body

/html

然后我给你一个test.php的文件代码:

?php

$name    =    trim($_POST[‘name’]);

$vipid    =    trim($_POST[‘vipid’]);

$con = mysql_connect(“127.0.0.1″,”数据库用户名”,”数据库密码”);

if (!$con)

  {

  die(‘Could not connect: ‘ . mysql_error());

  }

$a    =    mysql_select_db(“数据库名字”, $con);

$sql    =    “select * from kh_customer where name = ‘$name’ and vipid = ‘$vipid'”;

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))

  {

  echo $row[‘name’] . ” ” . $row[‘data’];

  echo “br /”;

  }

mysql_close($con);

?

页面美化自己去搞!只能帮你这么多了