Jump to content

MongodB to Mysql covertion help needed


cobusbo

Recommended Posts

Hi, Ive stumbled apon a Mongo database connected to a broadcast script. I would like to change it to a Mysql database can anybody please show me an example how it will look in Mysql format

<?php
/* Require the PHP wrapper for the Mxit API */
require_once ('MxitAPI.php');


/* Function to count the number of users in MongoDB */
function count_users() {
    $mongo = new Mongo('127.0.0.1');
    $collection = $mongo->sampleapp->users;
    $collection->ensureIndex(array('uid' => 1));


    return $collection->count();
}


/* Function to get batches of users from MongoDB */
function get_users($skip=0, $limit=50) {
    $mongo = new Mongo('127.0.0.1');
    $collection = $mongo->sampleapp->users;


    $collection->ensureIndex(array('mxitid'     => 1,
                                   'created_at' => 1));


    $users = $collection->find()->sort(array('created_at' => 1))->skip($skip)->limit($limit);


    return iterator_to_array($users);
}


/* Instantiate the Mxit API */
$api = new MxitAPI($key, $secret);


/* Set up the message */
$message = "(\*) Congratulations to our winners (\*)\n\n";
$message .= "1st place - Michael with 100 points\n";
$message .= "2nd place - Sipho with 50 points\n";
$message .= "3nd place - Carla with 25 points\n\n";
$message .= 'Good Luck!  $Click here$';


/* Mxit Markup is included in the message, so set ContainsMarkup to true */
$contains_markup = 'true';


/* Count the number of users in the database */
$count = count_users();


/* Initialise the variable that counts how many messages have been sent */
$sent = 0;


/* Keep looping through the user list, until the number of messages sent equals the number of users */
while ($sent < $count) {
    /* Get users in batches of 50 */
    $users = get_users($sent, 50);


    /* The list where the user MxitIDs will be stored */
    $list = array();


    foreach ($users as $user) {
        $list[] = $user['mxitid'];
        $sent++;
    }


    /* If there is a problem getting an access token, retry */
    $access_token = NULL;
    while (is_null($access_token)) {
        /* We are sending a message so request access to the message/send scope */
        $api->get_app_token('message/send');
        $token = $api->get_token();
        $access_token = $token['access_token'];


        // Only attempt to send a message if we have a valid auth token
        if (!is_null($access_token)) {
            $users = implode(',', $list);
            echo "\n$sent: $users\n";


            $api->send_message($app, $users, $message, $contains_markup);
        }
    }
}


echo "\n\nBroadcast to $sent users\n\n";
Link to comment
https://forums.phpfreaks.com/topic/291315-mongodb-to-mysql-covertion-help-needed/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.