Jump to content

trouble getting text to display


boblee

Recommended Posts

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

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)

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)

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)) { 

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.