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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.