<?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 are the differences between Get and post methods in form submitting, give the case where we can use get and we can use post methods?

11 Answers   HCL, Rushmore Consultancy, Thursday Technology,


What are default session time and path?

0 Answers  


What is the use of htmlspecialchars in php?

0 Answers  


What is api example?

0 Answers  


How to redirect https to http url through .htaccess?

0 Answers  






Do you know what is use of count() function in php?

0 Answers  


What is the best php version for wordpress?

0 Answers  


what are the rules to be followed at the time of declaring a variable?,what is the purpose of var_dump()function in php,syntax of ternary operator()in php,difference between drop a table and truncate a table...

2 Answers  


Tell us how to set cookies in php?

0 Answers  


/temp is a type of filesystem directory. State Whether True or False?

0 Answers  


Which function Returns the time of sunrise for a given day / location in PHP.

0 Answers  


how can i find number of rows in a table using MS Access and php? i used odbc_num_rows($query); but it returned -1.

1 Answers  


Categories