如果有很多请求,如何获取数据

时间:2012-08-16 11:40:00

标签: php mysql json time

这个topic与我在dba.stackexchange.com上的其他问题很相似,但问题不同

我的数据库中有两个表,一个是user_app表,其中存储了用户信息,表格如下:

+--------------+--------------+------+-----+-------------------+-----------------------------+
| Field        | Type         | Null | Key | Default           | Extra                       |
+--------------+--------------+------+-----+-------------------+-----------------------------+
| id           | int(11)      | NO   | PRI | NULL              | auto_increment              |
| apikey       | varchar(32)  | NO   |     | NULL              |                             |
| app_id       | varchar(16)  | NO   |     | NULL              |                             |
| appidt       | varchar(100) | NO   |     | NULL              |                             |
| imei_num     | varchar(32)  | NO   | MUL | NULL              |                             |
| app_version  | varchar(20)  | NO   |     | NULL              |                             |
| package_name | varchar(100) | NO   | MUL | NULL              |                             |
| stamp        | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| sdk_version  | float        | NO   |     | NULL              |                             |
+--------------+--------------+------+-----+-------------------+-----------------------------+

其他表格是创意表格,我可以通过该表格检索用户的广告:

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| campaign_id  | tinyint(4)   | NO   |     | NULL    |                |
| status       | tinyint(4)   | NO   |     | NULL    |                |
| type         | varchar(20)  | NO   |     | NULL    |                |
| title        | varchar(40)  | NO   |     | NULL    |                |
| description  | varchar(100) | NO   |     | NULL    |                |
| iconlink     | text         | NO   |     | NULL    |                |
| marketlink   | text         | NO   |     | NULL    |                |
| app_id       | varchar(16)  | YES  |     | NULL    |                |
| package_name | varchar(200) | YES  | MUL | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

这是我的php代码:

    $rss = $mysqli->query(" SELECT id as cid,
                        title, description, 
                        iconlink, marketlink, 
                        package_name FROM `creative` 
                        WHERE package_name <> '".$pkg."' AND id = 16 "
                    );

$arr = array();

while($obj = $rss->fetch_object()) {
    $arr = $obj;
    }
        //$uappid = $arr -> uid; //user app id for track
        $cid = $arr -> cid; // creative id
        //$campid = $arr -> campaign_id;
        $title = $arr -> title;
        $desc = $arr -> description;
        $ilink = $arr -> iconlink;
        $mlink = $arr -> marketlink;

$data = array('id'=>"$cid", 'title'=> "$title", 'description'=> "$desc", 'iconlink'=> "$ilink",'marketlink'=>"$mlink");

        $creative = json_encode($data); //return creative json if imei is there

        $response = array('success' => "true", 'result'=>"$creative", 'delay'=>"120000"); 

        echo json_encode($response); //return ad in json

在这种情况下,我必须在24小时内向用户发送一个广告,我将以json格式发送。此外,如果用户安装了我们的不同应用,我只需要在一整天内向他们发送一个广告。在这里,package_name代表我们的应用程序,我试图通过使用标志时间条目来实现这一点,如下所示:

$flag = date('dmy');

但如何每天实现单个广告,我只是对此进行研究。请看看这个问题。

更新:我很抱歉,我之前没有提及,但我想在广告请求中向用户发送一个广告。例如,用户可以发送请求数量并安装我们的应用程序,然后他不会在每个请求上获得相同的广告。他刚刚收到一个广告。

1 个答案:

答案 0 :(得分:1)

尝试添加结果查询的多维数组:

while($obj = $rss->fetch_object()) {
     $array[$obj->cid]['cid'] = $obj->cid;
     $array[$obj->cid]['title'] = $obj->title;
     $array[$obj->cid]['description'] = $obj->description;
     $array[$obj->cid]['iconlink'] = $obj->iconlink;    
     $array[$obj->cid]['marketlink'] = $obj->marketlink; 
     $array[$obj->cid]['package_name'] = $obj->package_name; 
    }


    echo '<pre>';
    var_dump(json_encode($array)); //return ad in json
    echo '</pre>';