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

4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Grabbing Comments From DB

Post by 4Boredom »

I have a comments DB withe the following vars

CommentID CommentLOC CommentTO CommentFROM CommentMESSAGE CommentDATE

I need to list CommentFrom and CommentMessage for CommentTO user every time its on their page
[url]http://www.4boredom.com/derek[url]

no clue what im doing here

Code: Select all

$display= mysql_query("SELECT * FROM `comments` WHERE `CommentTO`=".$profileid."");
print("<a href=\"viewprofile?userid=".$profileid."&CommentID=index\">");

$row = mysql_fetch_array($display);
if(!empty($row['CommentTO'])){
	$output.='<img src="http://4boredom.com/com?commentid='.$row['CommentID'].'" />';
	print($output);
} else {
	print("No Comments");
}
print("</a>");
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Grabbing Comments From DB

Post by califdon »

Code: Select all

$display= mysql_query("SELECT * FROM `comments` WHERE `CommentTO`=".$profileid."");
print("<a href=\"viewprofile?userid=".$profileid."&CommentID=index\">");

$row = mysql_fetch_array($display);
if(!empty($row['CommentTO'])){
	$output.='<img src="http://4boredom.com/com?commentid='.$row['CommentID'].'" />';
	print($output);
} else {
	print("No Comments");
}
print("</a>");
Actually, you're fairly close, considering your comment.

At the end of the first line, you don't need the ."" after $profileid.
Are you sure you want $output to be an image tag? Does the URL reference a location where there is an image file?

Beyond that, can you be sure that there will be only one record per profile?

Assuming from your comment that you are a rank beginner, you've done pretty well. You might do well to learn some of the terminology, though. What you referred to as "vars" are field names, also called column names. When listing the structure of a table (the column names), the convention is to list them vertically and show their data types, like:
CommentID INT(11)
CommentLOC VARCHAR(20)
CommentTO VARCHAR(30)
CommentFROM VARCHAR(30)
CommentMESSAGE VARCHAR(240)
CommentDATE DATE

That's not very important in this case, but it's good to form the habit of doing it the way others expect it, for future use.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Alright Don I appreciate the feedback... I have offsourced a code where someone displayed the users images from a profile, and my $500 budget ran out so I am doing it myself now.

One thing though... there WILL be more than one comment per page, how does that affect the code? ... I have updated the code as such

Code: Select all

<?php 
define('profile','1');
define('protect','1');
require_once("/home/i4boredo/protected_html/main.php");



$display= mysql_query("SELECT * FROM `comments` WHERE `CommentTO`=".$profileid.""); 
print("<a href=\"viewprofile?userid=".$profileid."&CommentID=index\">"); 

$row = mysql_fetch_array($display); 
if(!empty($row['CommentTO'])){ 
        $output.='http://4boredom.com/com?commentid='.$row['CommentID'].''; 
        print($output); 
} else { 
        print("No Comments"); 
} 
print("</a>"); 



?>
if I dont have the "" after the profileid it doesnt work....
however.... check out
4boredom.com/derek. This just shows an inline page? :( ... im trying to use my programmers code from another section and transform it to finish the final stages of my site.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I might handle it this way...

Code: Select all

<?php
$sql = "SELECT `CommentFrom`, CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
if (!$result = mysql_query($sql))
{
    die(mysql_error());
}

// I AM REALLY NOT SURE WHAT YOU ARE TRYING TO DO HERE.
// IT LOOKS ALMOST LIKE YOU WANT ONE LINK WITH EVERY COMMENT
// INSIDE THAT LINK AND EACH COMMENT IS AN IMAGE THAT HAS 
// A VERY WIERD SRC
echo '<a href="viewprofile?userid=' . $profileid . '&CommentID=index">';

while ($row = mysql_fetch_array($result))
{
    if(!empty($row['CommentTO'])) {
        echo '<img src="http://4boredom.com/com?commentid='.$row['CommentID'].'" />';
    } else {
        echo 'No Comments';
    }
}
echo '</a>';
?>
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

This latest version seems closer...

it outputs:

NoCommentsNoCommentsNoComments

lol.... except theres 3 entries for User 1 in the database so it should bring results?

http://4boredom.com/derek
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Check your negation operator (!). I bet it is missing.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

What is a negation operator? The wikipedia result doesnt have anything jumping out.....

::kicks self for being at an I.T. program at a University with no PHP courses::
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

I think he's referring to this line:

Code: Select all

if(!empty($row['CommentTO'])) {
the ! before empty is saying 'If not empty $row['CommentTO'] then' ... so basically if the $row['CommentTO'] has data go ahead and process it.

make sure the ! is in your code...

Thats my take on it...

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

Post by RobertGonzalez »

Wade got it...

Code: Select all

<?php
// Run the fetch array result set in a while loop
while ($row = mysql_fetch_array($result))
{
   // If the CommentTO field value is not empty...
   if (!empty($row['CommentTO'])) {
        // Echo this out
        echo '<img src="http://4boredom.com/com?commentid='.$row['CommentID'].'" />';
    } else {
        // Otherwise, if it is empty, echo this out
        echo 'No Comments';
    }
} 
?>
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Nope.... the ! has been in the code since I began the post... its not that
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Ive been working on this for an hour and cleaned it up a little but.. again... what I am attempting to do is grab CommentFROM and CommentMESSAGE from the database for each user and put them in a list on their profile. I have a thing, CommentTO that states what user its sent to... so say id=1 has 3 posts in DB... it should grab all 3 and post them...

ill deal with time later I cant even get the basics working yet

this is where were at 'NoComment NoComment NoComment' as an output

Code: Select all

<?php 
define('profile','1');
define('protect','1');
require_once("/home/i4boredo/protected_html/main.php");


$sql = "SELECT `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)) 
{ 
    if(!empty($row['CommentTO'])) { 
        echo '"http://4boredom.com/com?commentid='.$row['CommentID'].'"'; 
    } else { 
        echo 'No Comments'; 
    } 
} 
echo '</a>'; 



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

Post by RobertGonzalez »

Just for testings sake, can you try this...

Code: Select all

<?php
$c = 0;
while ($row = mysql_fetch_array($result))
{
    echo $row['CommentTO'] . ' is the value for CommentTO at loop position ' . $c . '...<br />';
    $c++;
} 
?>
Tell us what that shows you.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/i4boredo/public_html/comment-show.php on line 8
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sorry, probably should have been more clear...

Code: Select all

<?php

define('profile','1');
define('protect','1');
require_once("/home/i4boredo/protected_html/main.php");


$sql = "SELECT `CommentFrom`, `CommentMessage` FROM `comments` WHERE `CommentTO`= $profileid";
if (!$result = mysql_query($sql))
{
    die(mysql_error());
}


echo '<a href="viewprofile?userid=' . $profileid . '&CommentID=index">';
/**
 * TESTING THINGS HERE
 */
$c = 0;
while ($row = mysql_fetch_array($result))
{
    echo $row['CommentTO'] . ' is the value for CommentTO at loop position ' . $c . '...<br />';
    $c++;
}
/**
 * END TESTS
 */
echo '</a>';
?>
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

is the value for CommentTO at loop position 0...
is the value for CommentTO at loop position 1...
is the value for CommentTO at loop position 2...
Post Reply