Jump to content


Custom Function Problem


2 replies to this topic

#1 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 31 August 2007 - 03:36 PM

Aye up.

I've made this function Note: Functions aren't a strong point for me, yet)

// Query to get all PM's, new or old, limited by #
function pmGet($id, $get, $limit) {
	// $get defauled to 'all'
	// 'new' shows the newest PM's
	// 'newold' gets all PM's but displays new one's first
	switch($get) {
		// All
		case "all" :
		  $query = mysql_query("SELECT * FROM private_msg WHERE sent_to = '".$id."' ORDER BY date DESC LIMIT 0, $limit") or die(mysql_error());
		break;
		// New
		case "new" :
		  $query = mysql_query("SELECT * FROM private_msg WHERE is_new = '1' AND sent_to = '".$id."' ORDER BY date DESC LIMIT 0, $limit") or die(mysql_error());
		break;
		// New Old
		case "newold" :
		  $query = mysql_query("SELECT * FROM private_msg WHERE is_new = '1' AND sent_to = '".$id."' ORDER BY is_new AND date DESC LIMIT 0, $limit") or die(mysql_error());
		  break;
	}
	// Fetch results
	$row = mysql_fetch_object($query);
	// Return
	return $row;
}
// Get all PM's in a variable
$smarty->assign('getPMs', pmGet($_COOKIE['userID'], 'new', 5));

Urm... basically, depending on what $get is, it will display your messages.

When I output it, I don't get any results, I get this: Which I've never seen before

Quote

Object id #2

Anybody know what might be wrong?

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 31 August 2007 - 04:34 PM

An object is returned by mysql_fetch_object(); thus it's name. To access certain parts of your result row, you'll need to use the normal object way of getting values.

return $row->userID; // Or whatever column from the database you are grabbing

The alternative would be to simply use an array and use mysql_fetch_array() if you are more comfortable with those than objects.

#3 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 31 August 2007 - 06:38 PM

lol, it'd odd, I use mysql_fetch_object over the other options, about 90% of the time I still use $row['meh']... dur.

Anyway I sorted it by adding:

$pm = pmGet($_COOKIE['userID'], 'new', 5);
echo $pm->subject; // or whatever

Cheers for pointing out the error... this can be closed, or whatever.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users