Reputation: 5452
Im new to classes.
My Class:
if(!class_exists('my_connections')) {
class my_connections {
/* --------------- CHECKS IF CONNECTION EXISTS */
public function is_connection($member_id, $connection_id) {
global $db;
$sql = "SELECT * FROM `my_connections` WHERE `member_id` = '".$member_id."' AND `connection_id` = '".$connection_id."'";
$result = @mysql_query($sql,$db); check_sql(mysql_error(), $sql, 0);
//echo '<pre>'. $sql .'</pre>';
if(mysql_num_rows($result) > 0) {
echo "YES THEY ARE FRIENDS";
//$return = true;
} else {
echo "NO THEY ARE NOT FRIENDS";
//$return = false;
}
//return $return;
//echo $return;
} // EO method - is_connection
} // EO class
} //EO if !class_exists
Instantiate here:
$mc = new my_connections;
$test = $mc->is_connection('2154', '139');
echo $test;
It returns the echo statements just fine, but is not returning boolean,
for eg: instead of
echo $test;
I want to run a check against the method:
like so:
if ($test) {
//do something
}
the echo statements inside the method will be replaced with return true / false
Upvotes: 0
Views: 99
Reputation: 5250
Try this:
if(!class_exists('my_connections')) {
class my_connections {
public function is_connection($member_id, $connection_id) {
global $db;
$sql = "SELECT * FROM `my_connections` WHERE `member_id` = '".$member_id."' AND `connection_id` = '".$connection_id."'";
$result = @mysql_query($sql,$db); check_sql(mysql_error(), $sql, 0);
return mysql_num_rows($result) > 0 ;
}
}
And after check:
if(is_connection($member_id, $connection_id)){
// do something
}
Upvotes: 0
Reputation: 2224
Modified your class:
if(!class_exists('my_connections')) {
class my_connections {
/* --------------- CHECKS IF CONNECTION EXISTS */
public function is_connection($member_id, $connection_id) {
global $db;
$sql = "SELECT * FROM `my_connections` WHERE `member_id` = '".$member_id."' AND `connection_id` = '".$connection_id."'";
$result = @mysql_query($sql,$db); check_sql(mysql_error(), $sql, 0);
if(mysql_num_rows($result) > 0) {
echo "YES THEY ARE FRIENDS"; // message is echoed when you call this method
return true; // your variable is set
}
else {
echo "NO THEY ARE NOT FRIENDS";
return false;
}
}
} // EO class
} //EO if !class_exists
And your code:
$mc = new my_connections;
$test = $mc->is_connection('2154', '139'); // will automatically echo something from the class and set $test to true or false
Upvotes: 0
Reputation: 439
modify your code as below
if(!class_exists('my_connections')) {
class my_connections {
/* --------------- CHECKS IF CONNECTION EXISTS */
public function is_connection($member_id, $connection_id) {
global $db;
$sql = "SELECT * FROM `my_connections` WHERE `member_id` = '".$member_id."' AND `connection_id` = '".$connection_id."'";
$result = @mysql_query($sql,$db); check_sql(mysql_error(), $sql, 0);
//echo '<pre>'. $sql .'</pre>';
if(mysql_num_rows($result) > 0) {
// echo "YES THEY ARE FRIENDS";
return true;
} else {
//echo "NO THEY ARE NOT FRIENDS";
return false;
}
//return $return;
//echo $return;
} // EO method - is_connection
} // EO class
} //EO if !class_exists
Upvotes: 0