wordpress 3.2.1 database query - select

i am trying to make a simple selection in a wordpress table (created by a plugin). the table is named reduceri , and has the following columns: id, post, category.
so, i am trying to take all the category values, when the post is equal to the current post id.
the way i am doing the query is:
$the_query = "
SELECT $wpdb->reduceri.category
FROM $wpdb->reduceri
WHERE $wpdb->reduceri.post = ".$post_id."
";
$my_reduceri = $wpdb->get_results($the_query);
but when i var_dump the $my_reduceri all i get is an empty array: array(0) { } even though there should actually be some results... any idea where i am wrong (in the query)?
thank you

Did you declared global $wpdb; before using this query?

Related

CKan toolkit.get_action function is giving empty list when sorting by package_count

I'm new to CKAN and encountered a problem with template helpers. Particularly in my case, I will have to invoke toolkit.get_action('group_list') in my own template helper. However, when I add the constraint like the following:
results = toolkit.get_action('group_list')(data_dict={'sort': 'package_count desc',
'type': 'MyType',
'all_fields': True})
The results that I get back is an empty list. If I remove the 'sort' constraint from the data_dict, I can get the results of list the groups with 'MyType'. I don't know what caused this problem, because when I followed ckan toolkit official examples, it works for without any problems. However, what I can think of is this customized group might have its own schema such that package_count can not be used as a sort key. Since there's no error message, I can't make further assumption.
I figured out my own problem. The implementation I've showed in my question is correct. However, the database is not being populated properly. Basically you have to populate some packages for the particular groups. If you have no packages within each groups this function will return an empty query result. With the advice of #DRead, I researched the source code of _group_or_org_list(). If you take a look at this piece of code:
if sort_info and sort_info[0][0] == 'package_count':
query = model.Session.query(model.Group.id,
model.Group.name,
sqlalchemy.func.count(model.Group.id))
query = query.filter(model.Member.group_id == model.Group.id) \
.filter(model.Member.table_id == model.Package.id) \
.filter(model.Member.table_name == 'package') \
.filter(model.Package.state == 'active')
else:
query = model.Session.query(model.Group.id,
model.Group.name)
If you don't have packages created for groups, filter(model.Member.table_id == model.Package.id) will filter all groups out.

getting the relation data from where clause in view

I have the following query
$partner = DeliveryPartner::with(['deliveryPartnerImage' => function($q) {
$q->where('image_type', '=', 'logo');
}])
->find($id);
this works. for a partner with a logo it shows a relation with 1 item like so
and when i change the where clause to something non existent for example $q->where('image_type', '=', 'testtesttest');
it shows a empty array like this
so i know my query works but how to get this data in the view?
i did $partner->deliveryPartnerImage()->first() But for some reason this always shows data? also when i change the query... so im guessing im doing this wrong but i can not find another way to do this?
Use this:
$partner->deliveryPartnerImage
$partner->deliveryPartnerImage()->first() executes a new query without the where() constraint.

Pymongo insert and query reference

I have 2 table and I want to insert a reference to the first table into the second table. This is the table I want to reference, named player
self.db.insert('player',
{'char_id': char_info.char_id,
'name': char_info.name,
}
I tried following the official docs and do this:
self.db.insert('admin', {'char_id': {'$ref' : 'player', 'char_id': char_id}})
However, when I try it I get his error bson.errors.InvalidDocument: key '$ref' must not start with '$'
They queries might look a bit odd but all they are customized they just defined like this insert('table name', query)
How do I properly do this and be able to expand player?
I encountered the very same problem.. But I didn't find right answers for it. However, I was able to make it work using DBRef..
from bson.dbref import DBRef
ref=DBRef(collection='player',id=char_id)
self.db.insert('admin', {'char_id':ref})
This should work..
The problem with your idea is that it the Id doesn't get recognized as an Id. THis is why you have to tell pyMongo, that it has to deal with it as a foreign Id. you can do that with ObjectId:
from bson.objectid import ObjectId
table2 = db.table2
table2_elem = {
'player': ObjectId('_idStringOfOPlayerElement')
}
table2.insert_one(table2_elem)

How can I display count of imagefield images in views?

I want to display the number of images uploaded to an imagefield, in a views field or views tpl.php. How can I do this? My imagefield is called boatimages. I tried this but it comes out as 0, not the correct number: < ? php print count($fields->field_boatimages) ?>
Ack. I do not think count() works like that.
Why not just do this using Views? Take a look at Arguments > Settings and you'll see 'display record count' which seems like all you would need for this.
My suggestion is install the devel module and use the function dpm to print the variable if you wanna know the structure (print_r() may work too). If count isn't working it's because, you are probably using it with the wrong data.
OR, you could just query the database for the field. I'm gonna provide you instructions for drupal 7 but drupal 6 should be similar.
Check the table field_data_field_boatimages. See how there's a list of your images related with a single entity_id
Then execute this query
SELECT COUNT(*) FROM `field_data_field_boatimages` WHERE entity_id = ###
Where ### is the entity_id you want to know. You can get it by looking for arg(1) if arg(0) == node in your page.
Now you just have to use php power to print thar result
$query = SELECT COUNT(*) FROM `field_data_field_boatimages` WHERE entity_id = :eid
$result = db_query($query, array(':eid', $nid))->fetchField();
echo $result;
Drupal 6 would be very similar. Just a little difference in the table names and the query syntax. For example using db_result instead of fetchField()
Anyway good luck!

How to count rows in Lift (Scala's web framework)

I want to add a property to my User model that returns the number of rows in the Project table that have a user Id of the user.
So something like this...
def numProjects = {
/* somehow get count from Project table
The straight sql would be:
SELECT COUNT(*) FROM projects WHERE userId = <the current user>
*/
}
According to the documentation here (found here), assuming you're looking for the project count for a User of id 1234 and assuming that your Project model inherits the MetaMapper trait (probably through KeyedMetaMapper), it seems you can use the count method as such:
Project.count(By(User.id, 1234))
or
Project.count(BySql("userId = ?", 1234))
I can't test because I haven't used Lift yet, but it looks right... :) Let me know if it works!