jamesxg1 Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/ Share on other sites More sharing options...
Mchl Posted October 8, 2009 Share Posted October 8, 2009 You're getting any error messages? Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933254 Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Author Share Posted October 8, 2009 Hiya mate, No nothing thats whats confusing me, I get no feedback what so ever. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933258 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 while($comment = mysql_fetch_array($this->run_commentsql) { should be... while($comment = mysql_fetch_array($this->run_commentsql)) { missing a closing round bracket ')' make sure you close those bad boys. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933263 Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Author Share Posted October 8, 2009 Still got nothing :S. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933270 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933271 Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Author Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933280 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 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(); Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933288 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933298 Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Author Share Posted October 8, 2009 Ok, How do i see the contents of a array ?, Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933301 Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Author Share Posted October 8, 2009 I used print_r();. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933302 Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Author Share Posted October 8, 2009 Anyone :S, Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933307 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 what does print_r return? Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933310 Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Author Share Posted October 8, 2009 Array() Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933311 Share on other sites More sharing options...
mrMarcus Posted October 8, 2009 Share Posted October 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933320 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 thats the exact same code I told him to try. if it is returning just an empty array than there is something wrong with your query, or your query doesn't return anything. try echoing the query string and see what it says Quote Link to comment https://forums.phpfreaks.com/topic/177002-what-is-going-wrong-here/#findComment-933323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.