如何在php中使用for循环查询数据库而无需重载?

如何在php中使用for循环查询数据库而无需重载?,php,mysql,wamp,Php,Mysql,Wamp,我正在建立一个在线考试网站,当用户选择一个特定的考试 然后从数据库中获取的主题。然后计算主题的数量,然后将其分配给变量$no\u of_sub。 每个主题都有存储在变量$subject_cat[]中的子类别 $no_of_sub=7;//Here i assigned the no.of subject directly $subject_cat= array ( array('Indian History','Indian national movement'),

我正在建立一个在线考试网站,当用户选择一个特定的考试 然后从数据库中获取的主题。然后计算主题的数量,然后将其分配给变量$no\u of_sub。 每个主题都有存储在变量$subject_cat[]中的子类别

$no_of_sub=7;//Here i assigned the no.of subject directly
$subject_cat= array ( array('Indian History','Indian national movement'),
                      array('Indian and World Geography',' Indian Geography',
                            'World Geography','Geography(jammu and kashmir)' ), 
                      array('Indian Constitution','Indian Political History'), 
                      array('Economic and Social Development',
                            'Sustainable Development',' Poverty',
                            ' Inclusion', 'Demographics',
                            ' Social Sector  initiatives'), 
                      array('Environmental Ecology', 
                            'Bio-diversity and Climate Change','Physics ',
                            'Chemistry ','Biology','General Science'), 
                      array('Defence Technology','Space Technology',
                            'Nuclear Technology','Biotechnology','Health',
                            'Other Technologies'),
                      array('Current events'),    
                      array('General knowledge') );

        //$subject_cat is a multi dimension array having sub categories for each subjects

for($i=0; $i<$no_of_sub; $i++)
//loop for subjects till no of subjects
{
    for($j=0; $j<count($subject_cat[$i]); $j++)
       //loop for each sub category within a subjects using multi dimensional array  $subject_cat[subjects][sub_cat]
    {


        //Determine which subject to display the questions for
        $query = "SELECT ques_id, q, op1, op2, op3, op4
                  FROM questions where cat='".$subject_cat[$i][$j]."' limit 3";
        //for each subject category , it fetches data from MYSQL database Ex: where 

        cat="science"....
       //
       // other code goes here
       //
我的问题是如何防止这个问题,我想优化我的代码,因为 每次获取数据都需要很长时间,因为我的表中每个类别都有3000多行,所以php执行时间更长。 即使我将查询限制为1,查询也没有任何更改。 我只想在更短的时间内从每个类别和流程中选择一个问题。 任何帮助我的人都将不胜感激。

我找到了解决办法 for循环需要更多的执行时间,并且由于每个循环中的数据提取,服务器的数据缓存容量也会增加 因此,我现在使用
单独的函数。它现在可以正常工作。

构建一个查询并连接表。。因此,您只有一个查询和一个循环。请尝试使用一条sql语句检索所有类别的信息。每个考试都有不同数量的问题,根据用户选择,考试有不同类型的问题,从db查询。所有问题都在一个表@rufinusw中。您为什么不在sql查询中使用问题的第一行“主题Id”?我假设“主题id”是(主题、类别、问题)的唯一索引的一部分。此时查询将更加高效。
Where is the Indus Civilization city Lothal ? a) Gujarat b) Rajasthan c) Haryana d) Punjab

( ! ) SCREAM: Error suppression ignored for ( ! ) Fatal error: Maximum
execution time of 30 seconds exceeded in
C:\wamp\www\loginregister-master\testpage.php on line 68