doulu4233 2013-05-22 16:33
浏览 67

在我的类之外定义声明的变量

I have a class where I first do a SQL check to see if some certain data exist and if not then I INSERT a new record.

I'd like to somehow define a variable ($feedback) indicating ive either inserted something or havent - I want to use this var in a callback in my main PHP file (where ive included the class)

My class

class registerVote {

function registerVote(){

}

function init() {
    //Define some vars  
    return true;
}

function save() {
        //Sql count query
        $res = mysql_query($sql) or die( mysql_error());
        $count = mysql_result($res, 0); 

        //If no records are found then INSERT
        if($count < 1){
            //Some INSERT query
            $feedback = "new";
        } else {
            $feedback = "old";
        }

    if($res) {
        return $feedback;
    }
}

}

My PHP FILE:

include_once ('../class/class_vote.php');
$registerVote = new registerVote();

    if($registerVote->init()) {
        if($registerVote->save()) {
            //Print out my $feedback from the class
        if ($feedback == "new") {
            echo "new";
        } else {
            echo "old";
        }
        }
    }

The above naturally doesnt work - so im naturally wondering if im able to achieve what ive tried to illustrate?

  • 写回答

4条回答 默认 最新

  • doushi2845 2013-05-22 16:36
    关注

    You can have your registerVote::save() function return the $feedback. That seems like a reasonable way of doing it.

    Minor: if $res is false the or die case will have triggered and your script will exit, the if around the return isn't needed.

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch