Grabbing Comments From DB

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

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Psst.. only CommentFrom and CommentMessage will be in the result set. ;)
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Im just showing the result for what he asked me to process.


I have no clue why its not working...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

feyd wrote:Psst.. only CommentFrom and CommentMessage will be in the result set. ;)
4Boredom wrote:Im just showing the result for what he asked me to process.


I have no clue why its not working...
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.

Code: Select all

<?php
$sql = "SELECT `CommentFrom`, `CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
?>
The above query should be rewritten as

Code: Select all

<?php
$sql = "SELECT `CommentTO`, `CommentFrom`, `CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
?>
or the loop logic needs to be modified to check the CommentFrom field or CommentMessage field.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

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

Post by RobertGonzalez »

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:

Code: Select all

<?php
$c = 0;
?>
and you should be good to go.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

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>

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>'; 

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

4Boredom wrote:That did absolutely nothing to the output :(

Maybe people dont understand what is needed
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.

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()); 
} 
?>
Step 2: Build the HTML output

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>';
?>
If you plan out in plain simple language what you want to do then code to that, and try some stuff on your own, and use some simple logic, a lot of what you want to do will work for you. You are the one that knows what you want. We can help with the code, but at some point you need to put something into this of your own.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

omg it works im so happy

ty Everah I will remember you in a few weeks when I get this up... ill give you some free ad space
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're welcome.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

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

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>'; 


?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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. :wink:

As a last resort, you could always google 'MySQL Join queries'.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

any way this can be done without MySQL? I only know some PHP
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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

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'];
}
?>
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
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>';
} 
?>
Post Reply