Jump to content

Recommended Posts

Here i have a function that is used in my class i use every other function in this class and they work perfectly but this one isn't,

 

function GetComments() {

        $this->commentsql = "SELECT * FROM `comments` WHERE username_to = '$this->username' AND user_id_to = '$this->id'";
        $this->run_commentsql = mysql_query($this->commentsql) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);                                                                            
           return($this->run_commentsql);
}

 

I have tried putting

 

while($comment = mysql_fetch_array($new->GetComments()) {
echo $comment['comment'];
}

 

Iv tried this,

 

function GetComments() {

        $this->commentsql = "SELECT * FROM `comments` WHERE username_to = '$this->username' AND user_id_to = '$this->id'";
        $this->run_commentsql = mysql_query($this->commentsql) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);                                                                            

while($comment = mysql_fetch_array($this->run_commentsql) {

return($comment['comment']);

}

}

 

Nothing is working at all, Does anyone see any problems ?.

 

Many thanks,

 

James.

Link to comment
https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/
Share on other sites

well, if your query failed, it would give you an error..

 

this won't work because return leaves the function all together, so you would only get the first result row

while($comment = mysql_fetch_array($this->run_commentsql) {

return($comment['comment']);

}

 

you could build an array, and then return that array like so

$arr = array();
while($comment = mysql_fetch_array($this->run_commentsql) {

$arr[] = $comment['comment'];

}
return $arr;

you really, really should have error_reporting on, 'cause there's no way that script would execute without a closing ')'.

 

make sure when calling your function, you echo it since you are not returning an item creating by the function, rather an item generated by the function.  in the manner you're using the function, using echo instead of return would suffice.

 

using return;

echo GetComments();

 

using echo;

GetComments();

Nope, returns 'Array' when $arr is printed, This is really confusing me here :S.

 

Anyone have any idea to why this is not working ?.

 

Many thanks,

 

James.

 

um... of course it prints array if you just do

echo $arr.

 

you cant just print arrays like that. if you want to echo them you have to iterate though each entry...

function GetComments ($this->username, $this->id)
{
$this->commentsql = "SELECT * FROM `comments` WHERE username_to = '$this->username' AND user_id_to = '$this->id'";
    $this->run_commentsql = mysql_query($this->commentsql) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

while($comment = mysql_fetch_array($this->run_commentsql))
{ $arr[] = $comment['comment']; }

return $arr;
}
print_r ($arr).die;

 

what happens with that?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.