I have this code:
<?php
class guildData {
public $email = NULL;
public $hash_pw = NULL;
public $user_id = NULL;
public $clean_username = NULL;
public $display_username = NULL;
public function selectGuild($g_id)
{
global $db,$db_table_prefix;
$this->g_id = $g_id;
$sql = "SELECT
name
FROM
guild
WHERE
id = '".$g_id."'";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
return ($row['name']);
}
}
?>
<?php echo $guildData->selectGuild(1); ?>
I’m simply getting a 500 error and IDEone gave me this also:
Fatal error: Call to a member function selectGuild() on a non-object in /home/VT00Ds/prog.php on line 32
I can not see the error, can you help me?
You haven’t instantiated
$guildData. If you want to use this method without instantiating an object, the method should bestatic.Then you can call it from
Otherwise, you need to instantiate an object
Also, you should have some sort of
__construct()method in there, in order to set up your member variables.UPDATE I also noticed an error in your
selectGuild()method:Sets the value of
g_id, which is not defined as a member variable in the class. You must declareg_idas a member variable in your class definition:Finally,
sql_query()is not a PHP method that I’ve ever heard of. Unless you’re using a library that defines these methods, I think you meanmysql_query(). If this is the case, you should stop usingmysql_*functions. They’re being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you’re not sure which one to use, read this SO article.