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 )
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 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);
FQL query returning empty.
I run the following FQL in https://developers.facebook.com/tools/explorer:
SELECT uid, name, username, birthday_date, current_location, online_presence FROM user WHERE relationship_status ='Single' AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) LIMIT 30 OFFSET 0
She successfully returns all my friends who are single.
However, when I run the PHP, nothing is returned:
$fql = "SELECT uid, name, username, birthday_date, current_location, online_presence FROM user WHERE relationship_status ='Single' AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) LIMIT 30 OFFSET 0";
$solteiros = $facebook->api(array(
'method' => 'fql.query',
'access_token' => $userAccessToken,
'query' => $fql,
));
Until yesterday it was working fine.
Today when I test, it happened.
If it runs in the Explorer and not in your PHP app, you are most likely seeing a difference in access token clearance. That is, the access token supplied to Explorer has the permissions needed while the token used for your app does not.
Verify by calling /me/permissions on your PHP application to see what is there.
Your site also gives an invalid api key error
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 two tables in database namely QbQuestion(Qid,Question,StatusId) and Qbstatus(StatusId,Status),,,,,where Status stores status as new,acive,inactive etc.
i want to fetch StatusOptions on view form of QbQuestion in form of dropdownBox. i got succeded to fetch StatusOptions on QbQuestion view form but that selected entry is not getting inserted in QbQuestion table. In _form.php,to fetch StatusOptions, i have inserted code as follows:
labelEx(Qbstatus::model(),'Status'); ?>
findAll();
$list = CHtml::listData($records,'QuestionStatusId', 'Status');
echo CHtml::dropDownList('Qbstatus', null, $list, array('empty' => 'Select a Status'));
?>
error(QbStatus::model(),'Status'); ?>
So what i should do in order to make entries in QbQuestion table
Your question is not very clear, but from what I understand you are unable to make the drop down list display the correct options/values, which then prevents your QbQuestion table from storing the correct id for the QuestionStatusId?
Here is how your drop down list should look (untested obviously):
echo CHtml::dropDownList($model,'StatusId',CHtml::listData(Qbstatus::model()->findAll(),'QuestionStatusId','Status'),array('empty'=>'Select a Status'));
If you view the source you should see a normal HTML structure with Display Value
In your controller/action that receives the form, you should be able to echo out the model value for the StatusId and see there if it is getting passed