dongqu4443 2016-04-29 19:16
浏览 65

函数内的PHP函数[重复]

I created a simple function that needs to display some information.

Some of this information comes from the database.

But, the database handler is not recognized within this function, therefor, I get the following errors:

Notice: Undefined variable: db in censored\controller\edit_mails.php on line 82

Fatal error: Call to a member function get_results() on null in censored\controller\edit_mails.php on line 82

I'm using the ezSQL database handler

The function:

function MessageSelector() {
$emails = $db->get_results("SELECT * FROM settings_emails WHERE cat = '$edit_type'" );
if(empty($emails)) {
//There are no messages to select
        } 
else { 
        //Create an option for each message
        foreach ($emails as $email):  
            echo "<option value=\"", $email->title , "\" onclick=\"location.href='edit_emails.php?x=", $edit_type , "&mail=",$email->id,"';\">", $email->title , "</option>";
        endforeach;
        ";}  
}

Database Handler:

require_once ADMIN_DB_DIR . '/ez_sql_core.php';
require_once ADMIN_DB_DIR . '/ez_sql_mysql.php';
global $db;
$db = new ezSQL_mysql($dbuser, $dbpassword, $dbname, $dbhost);
</div>
  • 写回答

1条回答 默认 最新

  • duanhan3067 2016-04-29 19:22
    关注

    Pass in $db as a parameter into your function for it to be used in the scope of the function.

    Also, you have some weirdness in your foreach. Concatenate normally and don't forget to define $edit_type

    function MessageSelector($db) {
         $emails = $db->get_results("SELECT * FROM settings_emails WHERE cat = '$edit_type'" );
         if(empty($emails)) {
               //There are no messages to select
         } else { 
            //Create an option for each message
            foreach ($emails as $email) {  
                echo "<option value=\"". $email->title . "\" onclick=\"location.href='edit_emails.php?x=". $edit_type . "&mail=" . $email->id . "';\">". $email->title . "</option>";
            }
         }  
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)