Page 1 of 1
displaying a single value from a query
Posted: Fri Jul 28, 2006 11:43 am
by dandare
Hi
I was wondering if there is a way to use single values from a query without having repeat the query using WHERE.
This is my php code from the query.
Code: Select all
$tblAnswers =$_GET['tblAnswers'];
$result = mysql_query("SELECT answerName, questionName FROM tblAnswers");
if(!$result) die("Query Failed.");
while($row = mysql_fetch_array($result)) {
what I want to be able to do take single values from the questionName and answerName columns (not as a row).
Any help is appreicaitaed
Posted: Fri Jul 28, 2006 12:00 pm
by Jenk
So you want to 'search' the array, rather than the DB?
It's far, far more efficient to use the appropriate SQL statement.. this is what relational DB's are all about.
Anywho..
something like:
Code: Select all
$results = array();
while ($row = mysql_fetch_assoc($result)) {
if ($row['column'] == $value) {
$results[] = $row;
}
}
//$results now contain nothing but the rows that match the criteria.
Posted: Fri Jul 28, 2006 12:02 pm
by tcsoft
what I want to be able to do take single values from the questionName and answerName columns (not as a row).
what do you mean by that?
any select returns "rows" not values
EDIT:
btw, should be $tblAnswers in your query
Posted: Fri Jul 28, 2006 12:30 pm
by dandare
basically i want an easier way of displaying each answer separately (not as a list).
If i were to say
Code: Select all
<?php
$tblAnswers =$_GET['tblAnswers'];
$result = mysql_query("SELECT answerName FROM tblAnswers WHERE answerID = 1");
if(!$result) die("Query Failed.");
while($row = mysql_fetch_row($result)) {
echo '<p>' ,$row[1], '</p>';
}
?>
That is fine for displaying that one value. Though because there are many questions, would I have to copy out that whole statement again for the next row (answerID = 2), or is there a quicker way of doing this?
Sorry about this, I am very new to PHP
Thanx
Posted: Fri Jul 28, 2006 1:14 pm
by RobertGonzalez
What are you displaying? Are you displaying one question and all the answers related to it, or are you only displaying one question and one answer to it or all questions with all answers to each questions?
Posted: Fri Jul 28, 2006 2:07 pm
by dandare
I am displaying questions with multiple choice answers.
So for instance:
How many sides on a triangle?
4
6
3
5
The answers are in form of radio buttons.
I want to be able to retrieve the question name (how many...) and answerName (4 6...)
Also I'm thinking for the form values, it would be beneficial to use the choice of answer where A would represent 4;
B = 6 and so on.
Can you please tell me whether there is an easier way to do this.
At the mo I have:
Code: Select all
<?php
$tblAnswers =$_GET['tblAnswers'];
$result = mysql_query("SELECT answerName, questionName, answer FROM tblAnswers WHERE answerID <=4");
if(!$result) die("Query Failed.");
if($result) {
echo '<table align="center"><tr></tr>';
while($row = mysql_fetch_array($result)) {
echo "<tr><td></td><td align=\"left\"><p><b> $row[1] </b></p><br></td></tr>
<tr><td></td><td><p> $row[0] </p></td></tr>";
}
echo '</table>';
}
?>
for the first question.
Is there a more simplified way though?
Posted: Fri Jul 28, 2006 2:15 pm
by RobertGonzalez
Two loops: 1 loop for questions, 1 loop inside each question iteration for answers. I am assuming that you have databased your questions and possible answers to each question.
Posted: Fri Jul 28, 2006 2:20 pm
by dandare
ok cool I'll try that.
Thanks for the help.