如何用php获取数据库信息并显示

如何用php获取数据库信息并显示,第1张

获取ppq数据库的所有表名的代码:

?php

$server='localhost'

$user='root'

$pass='12345'

$dbname='ppq'

$conn=mysql_connect($server,$user,$pass)

if(!$conn)

die("数据库系统连接失败!")

$result=mysql_list_tables($dbname)

if(!$result)

die("数据库连接失败!")

while($row=mysql_fetch_row($result))

{

echo

$row[0]."

"

}

mysql_free_result($result)

?

mysql_list_tables

(PHP

3,

PHP

4

,

PHP

5)

mysql_list_tables

--

列出

MySQL

数据库中的表

说明

resource

mysql_list_tables

(

string

database

[,

resource

link_identifier])

mysql_list_tables()

接受一个数据库名并返回和

mysql_query()

函数很相似的一个结果指针。用

mysql_fetch_array()或者用mysql_fetch_row()来获得一个数组,数组的第0列就是数组名,当获取不到时

mysql_fetch_array()或者用mysql_fetch_row()返回

FALSE。

1、PHP获取显示数据库数据函数之 mysql_result()

mixed mysql_result(resource result_set, int row [,mixed field])

从result_set 的指定row 中获取一个field 的数据. 简单但是效率低.

举例:

$link1 = @mysql_connect("server1", 

"webuser", "password") 

or die("Could not connect 

to mysql server!")

@mysql_select_db("company") 

or die("Could not select database!")

$query = "select id, name 

from product order by name" 

$result = mysql_query($query)

$id = mysql_result($result, 0, "id")

$name = mysql_result($result, 0, "name")

mysql_close()

注意,上述代码只是输出结果集中的第一条数据的字段值,如果要输出所有记录,需要循环处理.

for ($i = 0 $i <= mysql_num_rows($result) $i++)

{

$id = mysql_result($result, 0, "id")

$name = mysql_result($result, 0, "name")

echo "Product: $name ($id)"

}

注意,如果查询字段名是别名,则mysql_result中就使用别名.

2、PHP获取显示数据库数据函数之mysql_fetch_row()

array mysql_fetch_row(resource result_set)

从result_set中获取整行,把数据放入数组中.

举例(注意和list 的巧妙配合):

$query = "select id, 

name from product order by name" 

$result = mysql_query($query)

while(list($id, $name) 

= mysql_fetch_row($result)) {

echo "Product: $name ($id)"

}

3、PHP获取显示数据库数据函数之mysql_fetch_array()

array mysql_fetch_array(resource result_set [,int result_type])

mysql_fetch_row()的增强版.

将result_set的每一行获取为一个关联数组或/和数值索引数组.

默认获取两种数组,result_type可以设置:

MYSQL_ASSOC:返回关联数组,字段名=>字段值 

MYSQL_NUM:返回数值索引数组.

MYSQL_BOTH:获取两种数组.因此每个字段可以按索引偏移引用,也可以按字段名引用.

举例:

$query = "select id,

 name from product order by name"

$result = mysql_query($query)

while($row = mysql_fetch_array

($result, MYSQL_BOTH)) { 

$name = $row['name']

//或者 $name = $row[1]

$name = $row['id']

//或者 $name = $row[0]

echo "Product: $name ($id)"

}

4、PHP获取显示数据库数据函数之mysql_fetch_assoc()

array mysql_fetch_assoc(resource result_set)

相当于 mysql_fetch_array($result, MYSQL_ASSOC)

5、PHP获取显示数据库数据函数之mysql_fetch_object()

object mysql_fetch_object(resource result_set) 

和mysql_fetch_array()功能一样,不过返回的不是数组,而是一个对象.

举例:

$query = "select id, name 

from product order by name"

$result = mysql_query($query) 

while($row = mysql_fetch_object

($result)) {

$name = $row->name

$name = $row->id

echo "Product: $name ($id)"

}

以上这些函数就是PHP获取显示数据库数据函数的全部总结。

这个可能是你在安装环境的时候出现了问题,在安装mysql的时候,数据库默认的不是gb2312,是西欧字符,建议你重新安装数据库,或是使用wampsever集成安装环境,默认数据库是utf8的,只要你建的数据库整理为utf8

网页文件也保存为utf8的

(不要bom头的那种)以后写php就很少出现问题啦。要是在php文件的最上面一行再加上这么一句代码,<?php

header("Content-Type:text/html

charset=utf-8")?>,那出现问题的几率就更小了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9923986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存