Page 1 of 2
Grabbing Comments From DB
Posted: Tue Feb 06, 2007 11:39 pm
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>");
Re: Grabbing Comments From DB
Posted: Wed Feb 07, 2007 12:01 am
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.
Posted: Wed Feb 07, 2007 10:30 am
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.
Posted: Wed Feb 07, 2007 1:46 pm
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>';
?>
Posted: Wed Feb 07, 2007 10:18 pm
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
Posted: Wed Feb 07, 2007 10:30 pm
by RobertGonzalez
Check your negation operator (!). I bet it is missing.
Posted: Thu Feb 08, 2007 5:00 pm
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::
Posted: Thu Feb 08, 2007 5:12 pm
by Wade
I think he's referring to this line:
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...

Posted: Thu Feb 08, 2007 5:45 pm
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';
}
}
?>
Posted: Thu Feb 08, 2007 5:54 pm
by 4Boredom
Nope.... the ! has been in the code since I began the post... its not that
Posted: Thu Feb 08, 2007 6:01 pm
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]
Posted: Thu Feb 08, 2007 6:07 pm
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.
Posted: Thu Feb 08, 2007 7:08 pm
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
Posted: Thu Feb 08, 2007 7:32 pm
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>';
?>
Posted: Thu Feb 08, 2007 8:07 pm
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...