我是新手。
我的班级:
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
在此实例化:
$mc = new my_connections;
$test = $mc->is_connection('2154', '139');
echo $test;
它返回echo语句就好了,但是没有返回boolean,
例如:而不是
echo $test;
我想对方法进行检查:
像这样:if ($test) {
//do something
}
方法内的echo语句将替换为return true / false
答案 0 :(得分:0)
修改您的代码,如下所示
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
答案 1 :(得分:0)
修改了你的课程:
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
你的代码:
$mc = new my_connections;
$test = $mc->is_connection('2154', '139'); // will automatically echo something from the class and set $test to true or false
答案 2 :(得分:0)
试试这个:
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 ;
}
}
检查后:
if(is_connection($member_id, $connection_id)){
// do something
}