21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
$a=array();
mysql_connect("localhost","root","123456");
mysql_select_db("test");
$rs=mysql_query("select id from aaa where name='a'");
while($row=mysql_fetch_array($rs))
{
$a[]=$row[0];
}
print_r($a);
echo " <br>";
$b=array_rand($a,2);//在数组$a中随机取得两个值并赋给数组$b
echo $a[$b[0]];//这种方式可以输出获得的值,但在sql语句中就会出现错误....
echo " <br>";
echo $a[$b[1]];
for($i=0;$i<2;$i++){
$query=mysql_query("select * from aaa where id='$a[$b[$i]]'");//sql语句中不能$a[$b[$i]]这么用么?该如何修改
$row=mysql_fetch_array($query);
}
print_r($row);
mysql_close();
?>
$id=$input[$rand_keys[0]];
$query=mysql_query("select * from aaa where id='$id'");
input = array("1", "2", "3", "4");
$rand_keys = array_rand($input, 2);
print $input[$rand_keys[0]] . "\n";
print $input[$rand_keys[1]] . "\n";
测试没出现你那为0的问题,你是不是$a中有0呢,print_r($a);看看
$input=array("1", "2", "3", "4");
$rand_keys = array_rand($input, 2);
for($i=0;$i<count($rand_keys);$i++){
$id=$input[$rand_keys[$i]];
$query=mysql_query("select * from aaa where id='$id'");
$result=mysql_fetch_array($query);
}
<?php
$input=array("1", "2", "3", "4");
$rand_keys = array_rand($input, 2);
print $input[$rand_keys[0]] . "\n";
print $input[$rand_keys[1]] . "\n";
$id=$input[$rand_keys[0]];
echo $id[0];
echo $id[1];
/*
$query=mysql_query("select * from aaa where id='$id'");
$result=mysql_fetch_array($query);
print_r($result);*/
?>