资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php随机调取数据库,php从数组中随机取一个数

php要是随机的从数据库中调取数据怎么实现啊,请指点

$sql = "select * from table";

成都创新互联公司:于2013年成立为各行业开拓出企业自己的“网站建设”服务,为1000+公司企业提供了专业的成都网站建设、网站制作、网页设计和网站推广服务, 定制网站设计由设计师亲自精心设计,设计的效果完全按照客户的要求,并适当的提出合理的建议,拥有的视觉效果,策划师分析客户的同行竞争对手,根据客户的实际情况给出合理的网站构架,制作客户同行业具有领先地位的。

$result = mysql_query($sql);

$rand = mt_rand(0,mysql_num_rows($result));

$i=0

while($rs = mysql_fetch_array($result)){

if($rand == $i){

$output = $rs[$i];

}

$i++;

}

$output数组就是随机记录

php语句,怎么从数据库中随机获取数据字段。

//连接数据库

if(!$con = mysql_connect("localhost","root","root")){die(mysql_error());}

mysql_select_db("ali_xt");

mysql_query('set names utf8');

//找出ali_admin表的字段

$res = mysql_query('show columns from ali_admin');

//将数据给弄出来

$data = array();

while ($row = mysql_fetch_assoc($res, MYSQL_NUM)) {

$data[] = $row;

}

//随机个数,默认5

$rand_times = 5;

$rand_times = count($data)$rand_times?count($data):$rand_times;

$result = array();

for( $i=0;$i$rand_times;$i++ ){

$result[] = $data[rand(0,count($data)-1)][0];

}

echo "pre";

print_r($result); //输出5个随机字段

mysql_close($con);

//纯手写的,不明白可以问我,记得给分

php程序随机调用mysql数据库的问题

select * from tableName where id 0 AND id 60 order by rand()

如果id有删除,用这个:

select * from (select * from tableName order by id ASC limit 0, 60 ) as tmp order by rand();

rand()是 mysql 自己的函数,取出数据后,按随机排序

php随机调用数据库中多个表数据如题 谢谢了

table width="50%" border="0" tr tdID/td tdID2/td tdorder1/td tdorder2/td tdorder3/td tdCAS/td /tr ?php //连接数据库 $conn=@ mysql_connect("127.0.0.1","root","root") or die("连接数据库失败!"); mysql_select_db("test",$conn) or die("连接数据库失败!"); mysql_query("set names 'GBK'"); //连接结束 //接收提交过来查询的ID $id=$_POST["id"]; //查询数据库相关数据 $sql="select * from test where ID2='".$id."' order by id desc"; // $sql="select a.*,b.* from test a inner join test2 b on a.ID2=b.ID2" ID2='".$id."' order by id desc; $query=mysql_query($sql); //循环输出 while($row=mysql_fetch_array($query)){ ? tr td?php echo $row["ID"];?/td td?php echo $row["ID2"];?/td td?php echo $row["order1"];?/td td?php echo $row["order2"];?/td td?php echo $row["order3"];?/td td?php echo $row["CAS"];?/td /tr ?php } ? /table form name="form1" method="post" action="" input type="text" name="id" input type="submit" name="Submit" value="查询" /form

PHP调用三种数据库的方法(3)

Oracle(甲骨文)是世界上最为流行的关系数据库。它是大公司推崇的工业化的强有力的引擎。我们先看看其相关的函数:

(1)integer

ora_logon(string

user

,

string

password)

开始对一个Oracle数据库服务器的连接。

(2)integer

ora_open(integer

connection)

打开给出的连接的游标。

(3)integer

ora_do(integer

connection,

string

query)

在给出的连接上执行查询。PHP生成一个指示器,解析查询,并执行之。

(4)integer

ora_parse(integer

cursor,

string

query)

解析一个查询并准备好执行。

(5)boolean

ora_exec(integer

cursor)

执行一个先前由ora_parse函数解析过的查询。

(6)boolean

ora_fetch(integer

cursor)

此函数会使得一个执行过的查询中的行被取到指示器中。这使得您可以调用ora_getcolumn函数。

(7)string

ora_getcolumn(integer

cursor,

integer

column)

返回当前的值。列由零开始的数字索引。

(8)boolean

ora_logoff(integer

connection)

断开对数据库服务器的链接。

以下是向ORACLE数据库插入数据的示例程序:

html

headtitle向ORACLE数据库中插入数据/title/head

body

form

action="?echo

$PHP_SELF;?"

method="post"

table

border="1"

cellspacing="0"

cellpadding="0"

tr

thID/th

thname/th

thDescription/th

/tr

tr

tdinput

type="text"

name="name"

maxlength="50"

size="10"/td

tdinput

type="text"

name="email"

maxlength="255"

size="30"/td

tdinput

type="text"

name="Description"

maxlength="255"

size="50"/td

/tr

tr

align="center"

td

colspan="3"input

type="submit"

value="提交" input

type="reset"

value="重写"/td

/tr

/table

/form

?

//先设置两个环境变量ORACLE_HOME,ORACLE_SID

putenv("ORACLE_HOME=/oracle/app/oracle/product/8.0.4");

putenv("ORACLE_SID=ora8");

//设置网页显示中文

putenv("NLS_LANG=Simplified_Chinese.zhs16cgb231280");

if($connection=ora_logon("scott","tiger"))

{

//库表test有ID,name,Description三项

$sql

=

'insert

into

test(ID,name,Description)

values

';

$sql

.=

'(''

.

$ID

.

'',''

.

$name

.

'',''.

$Description

.

'')';

if($cursor=ora_do($connect,$sql))

{

print("insert

finished!");

}

$query

=

'select

*

from

test';

if($cursor=ora_do($connect,$query))

{

ora_fetch($cursor);

$content0=ora_getcolumn($cursor,0);

$content1=ora_getcolumn($cursor,1);

$content2=ora_getcolumn($cursor,2);

print("$content0");

print("$content1");

print("$content2");

ora_close($cursor);

}

ora_logoff($connection);

}

?

/body

/html

PHP随机从数据库读取N条数据

不用从页面取id

去掉foreach

$sql="select * from phome_ecms_wma order by rand() limit 30"

就行了


分享名称:php随机调取数据库,php从数组中随机取一个数
网址分享:http://cdkjz.cn/article/dssdijg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220