displaying a single value from a query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dandare
Forum Commoner
Posts: 26
Joined: Wed Jul 26, 2006 7:56 am
Location: London

displaying a single value from a query

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
tcsoft
Forum Newbie
Posts: 12
Joined: Mon Jul 24, 2006 10:46 am
Location: Austria
Contact:

Post 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
dandare
Forum Commoner
Posts: 26
Joined: Wed Jul 26, 2006 7:56 am
Location: London

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
dandare
Forum Commoner
Posts: 26
Joined: Wed Jul 26, 2006 7:56 am
Location: London

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
dandare
Forum Commoner
Posts: 26
Joined: Wed Jul 26, 2006 7:56 am
Location: London

Post by dandare »

ok cool I'll try that.

Thanks for the help.
Post Reply