Grabbing Comments From DB
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
feyd wrote:Psst.. only CommentFrom and CommentMessage will be in the result set.
It is not working because, as feyd said, the CommentTO field is not being selected. You, I and the rest of the contibutors up to now had totally overlooked that point. Here is the query you are executing. Basically you are asking the database to serve you the CommentFrom column and the CommentMessage column from the table where the CommentTO value is equal to the value of the $profileid variable. But in the loop, where you are checking for $row['CommentTO'], it will always be null (or not set) because it is not coming out of the database, only CommentFrom and Comment Message are being served.4Boredom wrote:Im just showing the result for what he asked me to process.
I have no clue why its not working...
Code: Select all
<?php
$sql = "SELECT `CommentFrom`, `CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
?>Code: Select all
<?php
$sql = "SELECT `CommentTO`, `CommentFrom`, `CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
?>1 is the value for CommentTO at loop position 0...
1 is the value for CommentTO at loop position 1...
1 is the value for CommentTO at loop position 2...
Ok so this is the same result I was getting before the change... but 1 is my user id.... so it is now reading correctly from the CommentTO column
What are loop position 0 1 and 2 from this code? I see I need to work on that...
1 is the value for CommentTO at loop position 1...
1 is the value for CommentTO at loop position 2...
Ok so this is the same result I was getting before the change... but 1 is my user id.... so it is now reading correctly from the CommentTO column
What are loop position 0 1 and 2 from this code? I see I need to work on that...
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Read your code. Specifically the part in the loop that echo's out the part you are seeing on the screen. Remove the reference to $c inside the loop then remove the line that looks like:
and you should be good to go.
Code: Select all
<?php
$c = 0;
?>That did absolutely nothing to the output 
Maybe people dont understand what is needed I need to display it like this inside one TR for each entry
<td>UserName [CommentFROM]</td>
<td>Comment [CommentMESSAGE]</td>
Maybe people dont understand what is needed I need to display it like this inside one TR for each entry
<td>UserName [CommentFROM]</td>
<td>Comment [CommentMESSAGE]</td>
Code: Select all
<?php
define('profile','1');
define('protect','1');
require_once("/home/i4boredo/protected_html/main.php");
$sql = "SELECT `CommentTO`, `CommentFrom`, `CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
if (!$result = mysql_query($sql))
{
die(mysql_error());
}
echo '<a href="viewprofile?userid=' . $profileid . '&CommentID=index">';
while ($row = mysql_fetch_array($result))
{
echo $row['CommentTO'] . ' is the value for CommentTO at loop position <br />';
}
echo '</a>';
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
We understand completely. We also understand that you have been around for a while and that putting PHP output into simple HTML is something that you should already know how to do.4Boredom wrote:That did absolutely nothing to the output
Maybe people dont understand what is needed
Step 1: Get the database information:
Code: Select all
<?php
define('profile','1');
define('protect','1');
require_once("/home/i4boredo/protected_html/main.php");
$sql = "SELECT `CommentTO`, `CommentFrom`, `CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
if (!$result = mysql_query($sql))
{
die(mysql_error());
}
?>Code: Select all
<?php
echo '<table border="1">';
while ($row = mysql_fetch_array($result))
{
echo '<tr><td>' . $row['CommentFrom'] . '</td><td>' . $row['CommentMessage'] . '</td></tr>';
}
echo '</table>';
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Ok now I have this.
I want to turn the 1's for the Profile Id of poster... into the UserName
The $name column should read right...
How do I get CommerFROM to read from $name
I want to turn the 1's for the Profile Id of poster... into the UserName
The $name column should read right...
How do I get CommerFROM to read from $name
Code: Select all
<?php
define('profile','1');
define('protect','1');
require_once("/home/i4boredo/protected_html/main.php");
$sql = "SELECT `CommentTO`, `CommentFROM`, `CommentMESSAGE` FROM `comments` WHERE `CommentTO`= $profileid";
$name = "SELECT `username` FROM `users` WHERE `username` = `CommentFROM`";
if (!$result = mysql_query($sql))
{
die(mysql_error());
}
echo '<table border="0" width="100%">';
while ($row = mysql_fetch_array($result))
{
echo '<tr><td width="25%">' . $row['CommentFROM'] . '</td>
<td width="3%"></td><td width="72%">' . $row['CommentMESSAGE'] . '</td></tr>
<tr><td><br></td></tr>';
}
echo '</table>';
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
The easiest way would be to use a JOIN query on your comments table and user table. I am not going to tell you how because a) there are tons of examples all over these forums and b) it would be a great learning experience for you to try it and hit it.
As a last resort, you could always google 'MySQL Join queries'.
As a last resort, you could always google 'MySQL Join queries'.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Yeah, but you are going to need to get the data for the users anyway, that is why I suggested using a join. Regardless, you are going to need another query to get an array of usernames and ids. Make that a numerical array like
Then, in the code that you have already gotten to work, where it is outputting the userid, put that into the $users array as an index...
Code: Select all
<?php
$users = array();
// Do the query, get the result. Then loop
while ($row = mysql_fetch_array($result))
{
$users[$row['userId']] = $row['userName'];
}
?>Code: Select all
<?php
while ($row = mysql_fetch_array($result))
{
echo '<tr><td width="25%">' . $users[$row['CommentFROM']] . '</td>
<td width="3%"></td><td width="72%">' . $row['CommentMESSAGE'] . '</td></tr>
<tr><td><br></td></tr>';
}
?>