从两个表php中检索数据

时间:2015-03-02 03:46:06

标签: php mysql

我有两张表如下: activity_base(保存基本活动信息以及activity_type) activity_email(保存有关电子邮件活动的具体细节)

所有表都有activity_id,引用activity_base。

示例:我正在尝试输出电子邮件活动的数据,该活动根据活动类型从其他表中获取数据。

查询将引用activity_base以获取有关活动的基本信息,然后如果activity_type是电子邮件,我需要它通过运行函数email_activity_details从activity_email表中获取更多数据。

问题是:我无法将$ activity_details显示为未定义的变量。 这是查询: 电子邮件活动信息:

function email_activity_details($activity_id){
          global $connection;
          $email_activity = "SELECT * FROM activity_email WHERE activity_id ='$activity_id'"
                    or die("Error: ".mysqli_error($connection));
          $query_email_activity = mysqli_query($connection, $email_activity);
         return $query_email_activity;
        }

活动详情:调用email_activity_details():

function view_full_activity($activity_field){
            global $connection;

        $contact_id = $_REQUEST['contact_id'];
        $activity_id = $_REQUEST['activity_id'];
        $get = "SELECT * FROM activity_base WHERE activity_id = '$activity_id' "
                                    or die("Error: ".mysqli_error($connection));
        $query = mysqli_query($connection, $get);
    //Get activity base information
        while ($activity = mysqli_fetch_array($query)){
            $activity_related_to_id = $activity ['activity_related_to_id'];
            $activity_id = $activity['activity_id'];
            $activity_type_id = $activity['activity_type_id'];
    }     
    //Get detailed activity information 
      //If activity is Email
    if ($activity_type_id == "1") {
        $email_details = email_activity_details('$activity_id');
        while ( $email = mysqli_fetch_assoc($email_details)) {
          $activity_details = $email['email_message'];
        }
    }

switch ($activity_field) {
    case 'activity_id':
        return $activity_id;
        break;
    case 'activity_title':
        return $activity_title;
        break;
    default:
        # code...
        break;
}

}

希望我能够解释清楚。谢谢。

1 个答案:

答案 0 :(得分:1)

更改此行

$email_details = email_activity_details('$activity_id');

到这个

$email_details = email_activity_details($activity_id);

说明: 单引号内的美元符号按字面意思处理。

从PHP手册:

  

特殊字符的变量和转义序列在单引号字符串中出现时不会被展开

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single