English 中文(简体)
PHP和MSQL通过结果提出多份选举咨询查询和住宿
原标题:Multiple SELECT queries and looping through results in PHP and mySQL

I m trying to learn best practices with multiple SQL queries in PHP and mySQL. Not sure if I should have two separate queries or if I should join (or UNION?) my queries into one.

www.un.org/Depts/DGACM/index_spanish.htm 我能做以下工作,还是有另一种方式来完成同样的事情?

必须指出,我的第一个问询是把一个问答清单拉开。 第二点是把一份记分清单,根据用户的立分数向用户分配。 这两张表格之间的唯一关系是,这两张表格都使用与我“quiz”表中的ID有联系的同一外国钥匙。 我不需要日本海洋研究网旁边的这些表格,但我认为,我或许需要对这些表格进行联合国讨论,因此我的第二个询问结果在第一次之后出现。

现在,如果我把我的问答放在一起,我不知道我会如何通过问答结果来建立我的阵列。 当我得出每一项结果时,我需要某种有条件的逻辑,这种逻辑与我的第一份询问结果和我的第二次询问结果有所不同。 我如何写这些条件?

Here is what I m doing now that seems to work fine as two separate queries...

. 首次对数据库进行排序,以便了解我的问答:

$result_quiz = mysql_query("
    SELECT quiz_question.question_text, quiz_question.id, quiz_answer.answer_text, quiz_answer.points
    FROM quiz
    JOIN quiz_question ON (quiz.id = quiz_question.quiz_id)
    JOIN quiz_answer ON (quiz_question.id = quiz_answer.quiz_question_id)
    WHERE quiz.id = $id;
");

...then based on the above query,building my range of questions and questions:

$quiz = array();
while ($row = mysql_fetch_assoc($result_quiz)) {
    if (!isset($quiz[ questions ][ q .$row[ id ].  ])) {
        $quiz[ questions ][ q .$row[ id ].  ] = array(
             question  => $row[ question_text ],
             answers  => array()
        );
    }
    $quiz[ questions ][ q .$row[ id ].  ][ answers ][] = array( answer  => $row[ answer_text ], points  => $row[ points ]);
}

...then query the database for second summary:

$result_profile = mysql_query("
    SELECT quiz_profile.name, quiz_profile.description, quiz_profile.min_points, quiz_profile.max_points
    FROM quiz
    JOIN quiz_profile ON (quiz.id = quiz_profile.quiz_id)
    WHERE quiz.id = $id;
");

...then loop through my second query so, I can append the score profiles to my range:

while ($row = mysql_fetch_assoc($result_profile)) {
    $quiz[ profiles ][] = array( name  => $row[ name ], description  => $row[ description ], min_points  => $row[ min_points ], max_points  => $row[ max_points ]);
}

www.un.org/Depts/DGACM/index_spanish.htm 这是最佳办法吗?

www.un.org/Depts/DGACM/index_spanish.htm 或者,我是否把问询结合到一个使用联合国意见?。

If so, how can I write my conditions to know what results I m looping through?

感谢SO!

问题回答

我建议坚持目前的做法,除非存在履约方面的严重问题——将这两个问题合并起来可能会提高总投入(略为增加),但将大大提高显示这一方法所需的守则的复杂性。

I recommend to take a step back at the design of your app, not the code itself: If you go down the UNION road, you have to sacrifice all isolation between your questions-code and your scoreprofiles-code.

I say, this is not a good thing: What if one day the score profiles are in some way recalculated or trabsformed between db and user? Is it intuitive, that for that you have to touch the function getting the questions from the db?

在购买力平价中,有一条umb行规则,(所有umb行规则)并非100%完美,而是一开始: 与功能领域有关的所有代码都应放在一个文档中:include<>code>s和requires 反映属性。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...