<?php
include ("db.php");
$result = mysql_query("SELECT ques_id FROM questionbank
order by ques_id limit 5 ");

while($obj=mysql_fetch_array($result))
{
$ad1[$obj['ques_id']]++;//Used an array and inserted the
database query results into it.
}
$rand_keys=array_rand($ad1,1); //Did a random array function
echo "First random id = ".$ad1[$rand_keys[0]];
echo "<br>Second random id = ".$ad1[$rand_keys[1]];

?>
<!--Its not working. Have any solution for this. -->

Answer Posted / bibhu

The following is your program. you need some little bit
change on this program.
<?php
include ("db.php");
$result = mysql_query("SELECT ques_id FROM questionbank
order by ques_id limit 5 ");

while($obj=mysql_fetch_array($result))
{
$ad1[$obj['ques_id']]++;//
}
$rand_keys=array_rand($ad1,1); //Did a random array function
echo "First random id = ".$ad1[$rand_keys[0]];
echo "<br>Second random id = ".$ad1[$rand_keys[1]];

?>

Your Mistake
1. in while statement you add $ad1[$obj['ques_id']]++; which
is not assignment statement. It is a increment statement.
The correct answer is $ad1[$obj['ques_id']] = $obj['ques_id'];

2.in array random function You use 1 key but you display random
array value using 2 keys.

$rand_keys=array_rand($ad1,1); //Did a random array function
echo "First random id = ".$ad1[$rand_keys[0]];
echo "<br>Second random id = ".$ad1[$rand_keys[1]];

just change 1 to 2 in array_rand($ad1,2);


The Changes are from while statement.

while($obj=mysql_fetch_array($select))
{
$ad1[$obj['ques_id']] = $obj['ques_id'];
}
$rand_keys=array_rand($ad1,2); //Did a random array function
echo "First random id = ".$ad1[$rand_keys[0]];
echo "<br>Second random id = ".$ad1[$rand_keys[1]];

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why do we use query?

502


Explain some of the php string functions?

525


How can we extract string "pcds.co.in" from a string "https://info@pcds.co.in" using regular expression of php? More on reg can you explain

517


What is difference between array_merge and array_combine?

517


What is difference between include and include_once in php?

507






What is artisan in php?

503


What is the difference between explode() and split() functions?

498


How does one prevent the following warning ‘warning: cannot modify header information – headers already sent' and why does it occur in the first place?

509


What language is similar to php?

554


What is the difference between abstract class and interface in php?

523


How to declare an array in php?

539


Inside a php function, what param needs to be set in order to access a global variable?

537


How is php different from other languages?

477


What is list in PHP?

644


Is php the same as html?

546