Fetch total number of items from mysql through php outside gallery

filip_tc

Joined: 2006-05-25
Posts: 29
Posted: Tue, 2007-01-02 15:36

This is probably just a typical php mysql fetching question, but I have no experience using it so I figured I'd ask here and find out quicker.

The containing mysql table is 'g2_descendentcountsmap' inside the database 'gallery' and the rows are:

g_userId 	g_itemId 	g_descendentCount
       5               7                     4001
       6               7                     4001
                        ...

...where the I want to fetch the 4001 field ('g_descendentCount' column) from either of these two rows.

If possible, I would like to retrieve the number inside a variable (like $totalitems or similar) for subsequent use in math operations.

Thanks in advance,
Phil

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2007-01-02 17:17
 
filip_tc

Joined: 2006-05-25
Posts: 29
Posted: Tue, 2007-01-02 17:30

Thanks but I actually found out myself shortly after posting...

mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("gallery") or die(mysql_error());
$result = mysql_query("SELECT * FROM `g2_descendentcountsmap` WHERE `g_userId` = 5 AND `g_itemId` = 7")
or die(mysql_error());  
$row = mysql_fetch_array( $result );
$totalitems = $row['g_descendentCount'];
echo $totalitems .' total items';

Thanks anyways,
Phil

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Wed, 2007-01-03 03:39

If anyone later finds this topic:
- don't hardcode numbers like 5 (guest) or 7 (root album). use GalleryCoreApi::getDefaultAlbumId() and ::getAnonymousUserId().
- don't use proprietary mysql_ code since it's not portable to other DBs, use g2's $storage methods instead
- don't write SQL queries for things that are covered by the core API since the API is supported and versioned while your SQL code might just stop to work after an upgrade because things have been refactored in gallery.

 
filip_tc

Joined: 2006-05-25
Posts: 29
Posted: Sun, 2007-01-14 01:47

Hey valiant, I'm trying to get things right so I want to do this the 'correct' way. I have no clue how to do what you are saying... I want to execute the query from drupal (in the content of a page) through the gallery GalleryCoreApi... but I guess I'd have to php include a gallery core file so that the function can get called (or maybe not, since I am using the drupal gallery module???). What code would I need to do what you are saying?

Thx

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2007-01-15 16:19

the drupal integration for gallery already includes the gallery files and has the necessary functions to init gallery.
i'd look into the g2 integration for drupal and extending it for your needs.

--------------
Enter the Gallery 2 Theme Contest today!