I would like to select users from the Drupal users table where the date created was older than 1 day ago. Then delete those users. This code isn't correct but it will give you an idea of what I am trying to do I just don't know the right DB query.
'
$users = db_select('user', 'created') //I know this is not the right DB query
$count = count($users);
if($count > 0){
$users = user_load_multiple(array_keys($users['user']));
}
$today = getdate();
if ($users < $today - '100000' ) {
user_delete($user->uid)
}
I am thinking about using user_load_multiple() to load the users in to an array to perform the delete action.
Get an array with the uids of the users you want to delete and use user_delete_multiple($arrayOfUidToDelete) : https://api.drupal.org/api/drupal/modules!user!user.module/function/user_delete_multiple/7
Using the function user_load_multiple() can be heavy on ressources if you have a lot of users and you will end with PHP errors (out of memory...).
Plus you don't need to load the users, you just want to delete them.
For the query I will go with db_query instead of db_select if there is no parameters valued by an user (better performances).
So it will look something like :
//get the list of uid
$user_list = db_query("select uid
from users
where CAST(FROM_UNIXTIME(created) as date) < NOW() - INTERVAL 1 DAY"
)->fetchCol();
// delete the users
user_delete_multiple($user_list);
Related
I'm trying to figure out a way to use WPDB to load a whole row or single cells/fields from another table (not the Wordpress-DB) and displaying them in a shortcode. I have a bunch of weatherdata-values, I need the latest row (each column is another data-type (temp, wind, humidity, etc) of the database for a start.
Sadly, the plugin that would do everything that I need, SQL Shortcode, doesn't work anymore. I found this now:
https://de.wordpress.org/plugins/shortcode-variables/
Though I still need to use some PHP/PDO-foo to get the data from the database.
By heavy copy&pasting I came up with this:
<?php
$hostname='localhost';
$username='root';
$password='';
$dbname='sensordata';
$result = $db->prepare(SELECT * FROM `daten` WHERE id=(SELECT MAX(id) FROM `daten`););
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$data = $row['*'];
}
echo $data;
?>
But obviously it's not working. What I need to get it done with WPDB?
kind regards :)
Just in case anyone else needs this in the future. I used this now:
//connect to the database
<?php
$dbh = new PDO('mysql:host=localhost;dbname=databasename', 'dbuser',
'dbpasswort');
//query the database "databasename", selecting "columnname" from table "tablename", checking that said column has no NULL entry, sort it by column "id" (autoincrementing numeric ID), newest first and just fetch the last one
$sth = $dbh->query("SELECT `columnname` FROM `tablename` WHERE `columnname` IS NOT NULL order by id desc limit 1")->fetchColumn(0);
//print the value/number
print_r($sth);
?>
By using "SELECT colum1, colum2,... FROM" You should get all the columns, could be that fetchColumn needs to be replaced with something different though.
I am using CakePHP 3.x. I have converted and stored all my dates to string using the strtotime(date('Y/m/d h:i', time())); .And in DB it will store like this 1479466560 But Right now i need to select rows from exactly 14 days ago. I tried like below conditions,
$this->loadModel('Users');
$userList = $this->Users->find('all')
->select(['created'])
->where(['status' => 1,'created' => 'created + INTERVAL 14 DAY > NOW()']);
It is returning empty rows only. How to do this ?. Can any one help me !
I know this does not tell you how to use interval but it does solve the problem.
What I've done in the past is a simple >= < combination together with php variables:
$cur_date = date_create();
$date = clone $cur_date;
$date->modify("-12 hours");
$this->Model->find()
->where(['created >= ' => $date])
->andWhere(['created < '=> $cur_date]);
This effectively uses an interval. It is also nicely dynamically editable.
Your query doesn't match with any created date from your users table.
Your CakePHP3 query generated below SQL:
SELECT created FROM users WHERE (status = 1 AND created = created + INTERVAL 14 DAY > NOW())
Note: At First try and adjust above query from your phpmyadmin.
You can try below SQL for INTERVAL test
SELECT '2016-01-17 10:57:21' + INTERVAL 14 DAY > NOW();
OR
SELECT '2016-01-17 10:57:21' + INTERVAL 14 DAY;
Note: Adjust date string as you need. Now you can see what is the wrong you have done
I tried with many things and it always said the same i need return the user or the list of all users
i dont know what happen,
resul returns Resource id and
if i print mysql_fetch_array it returns Array and print r doesnt return all users in my database
<?php
,
$consulta = "SELECT user FROM usuarios";
$resul = mysql_query($consulta,$conexion);
echo $resul;
print_r(mysql_fetch_array($resul));
?>
with phpmyadmin return all users.. that query works fine when i put that with phpmyadmin
i have in that moment in the database one table called usuarios with 3 columns called user(admin,pablo) pass(admin,pablo) and obviously id
but the ouput is this
Resource id #2Array ( [0] => admin [user] => admin )
I have table called wp_email_subscription and inside table I have rows of emails from subscribed users. Each row contains id, email, and name column.
I have also created admin page for plugin where I would like to list all emails from subscribed table.
Basically I don't know how to use wpdb functions, I have read wpdb instructions from wp website but I don't understand.
what I need is to select all from wp_email_subscription and foreach row to display email and name column in list.
you have to use global wpdp variable and get_result function in your code page like this...
global $wpdb;
$row = $wpdb->get_results( "SELECT * FROM wp_email_subscription");
foreach ( $row as $row )
{ echo "email:".$row->email.";} //$row->your_column_name in table
like this, you can access all columns by $variable->col_name
I have an app where I want users to be able to buy in-game credits called coins.
I have set my callback url, and can make a purchase, I get an order_id and settled from the callback, but how do I update the users row in my mySql database after they have made the purchace.
Kinda how my mySql DB looks
ID......FBID............Name..........Coins
7.......1000***797....John..............0
After the user has bought 200 coins for 1 credit, I would like to do this
$SQL = "UPDATE tb_users SET Coins = Coins+200 WHERE FBID = '$usernumber'";
$result = mysql_query($SQL);
But how and where am I supposed to do that? Is it in the callback.php file or in the index.php file after "var callback = function(data)".
I can't seem to pass the user_id from the form on the index to callback.php
So how do I do that, and if that is possible, am I supposed to update my database in the callback.php file??
Nevermind found this out. You can get the info about the user via API the normal way and update the DB in callback.php after "settled".