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



<?php include ("db.php"); $result = mysql_query("SELECT ques_id FROM questionban..

Answer / 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

More PHP Interview Questions

What is PHP's configuration file called?

0 Answers  


What beforeFilter() is used?

0 Answers   HCL,


How to remove leading and trailing spaces from user input values in php?

0 Answers  


What is the difference between apache and tomcat?

0 Answers  


What is difference between core php and framework?

0 Answers  


What is the difference between super () and this ()?

0 Answers  


What is encapsulation in php?

0 Answers  


write the function to fetch a image from mysql databases (datatype blob).

1 Answers  


What is the function to count elements in an array in PHP?

0 Answers  


Here in this site i could not find the details for php ZEND certifications or i think u did not post any ? so i need the information about the php certification how to prepare and some of the mock exams?

1 Answers  


explain the changes in php version

3 Answers  


what is the scope of php in the future if any other language is developed then may be php is loss ???

1 Answers   ATI,


Categories