boblee Posted December 29, 2008 Share Posted December 29, 2008 Hey everyone, I've written a php file that is supposed to select a question from a database table and display both the question and answer. However only the question is being displayed and in place of the answer is Resource id #3 any ideas? Heres my code, Thanks a bunch <?php $query = mysql_connect("*****", "*****", "*****") or die(mysql_error()); mysql_select_db('JohnPiatt', $query) or die(mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>RefTek || FAQ</title> </head> <html> <body> <?php $conn = "SELECT question FROM `Q_A`"; $result = mysql_query($conn, $query); while ($row = mysql_fetch_row($result)) { $question[] = $row[0]; } foreach ($question as $v){ echo "<div>\n"; echo "<p class='faq_text_q'>\n" .$v. "</p>\n"; echo "</div>\n"; $sql = mysql_query("SELECT answer FROM Q_A WHERE question = '. $v .'"); echo "</div>\n"; echo "<p class='faq_text_a'>\n" .$sql. "</p>\n"; echo "</div>\n"; } ?> </body> </html> (removed db connection stuff) Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/ Share on other sites More sharing options...
redarrow Posted December 29, 2008 Share Posted December 29, 2008 example only correct the ['array_names'] <?php $query = mysql_connect("*******", "*****", "*****") or die(mysql_error()); mysql_select_db('JohnPiatt', $query) or die(mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>RefTek || FAQ</title> </head> <html> <body> <?php $conn = "SELECT * FROM `Q_A`"; $result = mysql_query($conn, $query); while ($row = mysql_fetch_assocc($result)) { echo "<div>\n"; echo "<p class='faq_text_q'>\n" .$row['quistion']. "</p>\n"; echo "</div>\n"; echo "</div>\n"; echo "<p class='faq_text_a'>\n" .$row['ansaw']. "</p>\n"; echo "</div>\n"; } ?> </body> </html> (removed db connection information) Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/#findComment-725249 Share on other sites More sharing options...
redarrow Posted December 29, 2008 Share Posted December 29, 2008 This way better. <?php $query = mysql_connect("*****", "*****", "*****") or die(mysql_error()); $res=mysql_select_db('JohnPiatt', $query) or die(mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>RefTek || FAQ</title> </head> <html> <body> <?php $conn ="SELECT * FROM `Q_A`"; $result = mysql_query($conn)or die(mysql_error()); while ($row = mysql_fetch_assocc($result)) { echo "<div>\n"; echo "<p class='faq_text_q'>\n" .$row['quistion']. "</p>\n"; echo "</div>\n"; echo "</div>\n"; echo "<p class='faq_text_a'>\n" .$row['ansaw']. "</p>\n"; echo "</div>\n"; } ?> </body> </html> Next time don't post database info ok. (removed db connection information) Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/#findComment-725250 Share on other sites More sharing options...
boblee Posted December 29, 2008 Author Share Posted December 29, 2008 I can't believe I posted my info again :-\, that what I get for posting @ 3 am. Anyway, thanks for the responses, I tried the code above and now all I am getting is a blank page. btw I replaced while($row=mysql_fetch_assocc($result)) { with while($row=mysql_fetch_assoc($result)) { Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/#findComment-725368 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2008 Share Posted December 29, 2008 You're not doing a fetch after the mysql_query in the second loop. Actually, you don't need a second loop or the temporary array. Try: <?php $query = mysql_connect("*****", "*****", "*****") or die(mysql_error()); mysql_select_db('JohnPiatt', $query) or die(mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>RefTek || FAQ</title> </head> <html> <body> <?php $conn = "SELECT question FROM `Q_A`"; $result = mysql_query($conn, $query); while ($row = mysql_fetch_assoc($result)) { echo "<div>\n"; echo "<p class='faq_text_q'>\n" . $row['question'] . "</p>\n"; echo "</div>\n"; $sql = mysql_query("SELECT answer FROM Q_A WHERE question = '. $row['question'] .'"); $r = mysql_fetch_assoc($sql) echo "</div>\n"; echo "<p class='faq_text_a'>\n" .$r['answer']. "</p>\n"; echo "</div>\n"; } ?> </body> </html> Ken Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/#findComment-725373 Share on other sites More sharing options...
boblee Posted January 4, 2009 Author Share Posted January 4, 2009 Hey, sorry it took me so long to reply, I was out of town, anyway, I tried the code above and now i'm getting this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/J/a/p/JapII/html/reftek/FAQ.php on line 19 this is line 19: $sql = mysql_query("SELECT answer FROM Q_A WHERE question = '. $row['question'] .'"); thanks for the help. Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/#findComment-729161 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2009 Share Posted January 4, 2009 Try this instead: <?php $q1 = "SELECT answer FROM Q_A WHERE question = '" . $row['question'] . "'"; $sql = mysql_query($q1); ?> Ken Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/#findComment-729312 Share on other sites More sharing options...
boblee Posted January 6, 2009 Author Share Posted January 6, 2009 thanks a bunch, for all your guys help Link to comment https://forums.phpfreaks.com/topic/138722-trouble-getting-text-to-display/#findComment-730613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.