Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to write if statement in MySQL
Old 08-31-2011, 02:34 PM How to write if statement in MySQL
Junior Talker

Posts: 1
Trades: 0
Hi everyone....
I have a table in my database comprising of 'yesnoquestion_id', 'student_id', 'student_answer', 'answer'.
I need to get some information out into my webpage interface using MySQL. I need to output the number of student whose answers answers are correct to the right answer into a table that will show on the interface..... the write answer is 'answer' while the students' answer is 'student_answer'.

Here is what i have done so far:

PHP Code:
<?php

global $connection;
$query = "SELECT yesnoquestion_id AS Question_No, if (answer = 1, 'TRUE', 'FALSE') AS Answer, Count(student_id) AS num1
FROM yesnoanswers
WHERE student_answer = answer
GROUP BY yesnoquestion_id
ORDER BY yesnoquestion_id ASC "
;
$collate_set = mysql_query($query, $connection);

//Drawing table and inserting data into it
echo "<table border='5'>
<tr>
<th>Question Number</th>
<th>Question Answer </th>
<th>No. of Student correct</th>
</tr>"
;
while (
$row = mysql_fetch_array($collate_set))
{
echo
"<tr>";
echo
"<td>" . $row ['Question_No'] . "</td> ";
echo
"<td>" . $row ['Answer'] . "</td> ";
echo
"<td>" . $row ['num1'] . "</td> ";
echo
"</tr>";
}
echo
"</table>";
?>

This is outputting just the 'YESNOQUESTION_ID', 'NUMBER OF STUDENTS', 'STUDENT_ANSWER = ANSWER'.

How can i get it to also output the 'STUDENT_ANSWER != ANSWER' i.e where 'student_answer' IS NOT 'answer' on the same table ?


Thank you
olaad is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-31-2011, 02:54 PM Re: How to write if statement in MySQL
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
First: MySQL is NOT php so this is the wrong place for the question.

Second: This
Code:
(answer = 1, 'TRUE', 'FALSE') AS Answer,
will throw an error if the server is running on a case insensitive OS such as Windows

Assuming that 'answer' is a column name, you CANNOT have a column AND an alias with the same identifier.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-31-2011, 11:06 PM Re: How to write if statement in MySQL
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Quote:
Originally Posted by olaad View Post
How can i get it to also output the 'STUDENT_ANSWER != ANSWER' i.e where 'student_answer' IS NOT 'answer' on the same table ?


Thank you
Removing WHERE student_answer = answer might help
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 09-12-2011, 09:34 AM Re: How to write if statement in MySQL
Novice Talker

Posts: 5
Trades: 0
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");

mysql_close($con);
?>

might help you
epixeltechno is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to write if statement in MySQL
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28132 seconds with 12 queries