I have written some PHP code that will return a value if there are results returned and return 0 if no results are returned. However, the function is very cumbersome and difficult to parse. I was wondering if there was a cleaner way to rewrite my code. Thanks in advance!
$last_question_sql="SELECT DISTINCT QUESTION_ID
FROM branching_survey_responses
WHERE QUESTION_ID开发者_运维技巧=(SELECT Max(QUESTION_ID)
branching_survey_responses
WHERE CUSTOMER_ID=232
AND SET_ID=2)
AND CUSTOMER_ID=232
AND SET_ID=3";
$last_question_result=mysql_query($last_question_sql);
if($last_question_status=mysql_fetch_assoc($last_question_result)){
$last_question=$last_question_status['QUESTION_ID'];
}
else{
$last_question= 0;
}
EDIT:
i'm not sure, but if you are using this code for get back the last inserted item you can just use mysql_insert_id()
example:
after the INSERT
query has successfully executed you can retrieve the last id like
$last_question = mysql_insert_id();
function last_question($cid, $sid1, $sid2)
{
$question = 0;
$result = mysql_query(
"SELECT DISTINCT QUESTION_ID
FROM branching_survey_responses
WHERE QUESTION_ID=(SELECT Max(QUESTION_ID) branching_survey_responses
WHERE CUSTOMER_ID={$cid}
AND SET_ID={$sid1})
AND CUSTOMER_ID={$cid}
AND SET_ID={$sid2}");
if ($status = mysql_fetch_assoc($result)) {
$question = $status['QUESTION_ID'];
}
return $question;
}
精彩评论