<?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

Explain about PHP cookies?

618


What are magic methods?

546


What’s the special meaning of __sleep and __wakeup?

563


Is php a programming language?

546


Define anonymous classes in php7?

555






How to get the number of characters in a string?

567


Is key in array php?

587


Which function would you use to merge two arrays in php?

573


What is session cookies php?

547


How to replace a substring in a given string?

550


What is the use of array_search() in php?

529


Tell me how a constant is defined in a php script?

476


How to delete a file from the system?

570


How to know user has read the email-php?

495


How do I make a reset button in html?

517