PDA

View Full Version : How to give everybody in db a one time amount of credits?


vodka00
04-09-2013, 22:07
Hello,

If I want to start using "first_connection_credits" "1000" for people that join from now on. Then I would also like to give all the people that were added to the database before this feature. So that it's fair for everyone.

Is there some sql code I can run through my database that will give everyone currently in it a set amount of credits? In this case 1000.

Thanks.

eXemplar
04-09-2013, 22:33
Something like this:
update store_users set credits = 1000 where credits < 1000

Arrow768
04-10-2013, 02:34
I would do it via php:
Create a file named update_users.php in application/controllers

<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class update_users extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('users_model');

if (!$this->ion_auth->logged_in() && $this->config->item('storewebpanel_enable_loginsystem') == 1) {
redirect('auth/login');
}
}

public function index() {
$DB_Main = $this->load->database("default", TRUE);
$query = $DB_Main->get("store_users");

foreach ($query->result_array() as $row) {
$this->users_model->add_credits($row['id'], "1000");
echo "gave user".$row["name"]."1000 credits </br>";
}
}

}

Then navigate to index.php/update_users and you should see something like:

gave userA 1000 credits
gabe userB 1000 credits


Once you have done this every user should have 1000 additional credits
if you want to change the amount of the credits just edit the 1000 to the number of credits you want to give

You could also use the sql mentioned by eXemplar, but then you just set the number of credtis to 1000 for everyone with less then 1000 credtis

exactprecisions
04-10-2013, 20:00
Yea I'm not sure if SQL allows you to store the present value in a variable and them add 1000 and then update, updating it or the same in a SQL query like the first person mentioned, would not add 1000 but instead replace whatever credits were earned already with 1000

It's best to do it with PHP or something similar so you could store the present value for each user in a variable and then add 1000 and store it to a new variable, then "update" it with a sql query with the new variables value

Arrow768
04-11-2013, 06:50
[...]
It's best to do it with PHP or something similar so you could store the present value for each user in a variable and then add 1000 and store it to a new variable, then "update" it with a sql query with the new variables value

And now guess what the code I have posted above does :)

Phault
04-11-2013, 09:27
UPDATE store_users SET credits = credits+1000; That should do it.

Arrow768
04-11-2013, 10:16
UPDATE store_users SET credits = credits+1000; That should do it.
Wow, thats easy.
I never thought that there is such an easy solution