#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9

    I miss a PHP feature: mysql_fetch_array! :(


    Well, I was wondering...
    In PHP, if you wanted to do some math with some mysql query results you could have done something like this:
    Code:
    while($row = mysql_fetch_array($result))
    {
       $data[] = $row['FirstFieldName'] + $row['SecondFieldName'];
    }
    And to say the truth, in python, I'm having some serious trouble seeing the resulting tuples form a mysql query similar to that PHP code. It seems to me like if in PHP, they where using something like "dictionaries" to iterate into the mysql results, so keys would be the table field names.
    Is python's way better? Because I really liked the while($row...) stuff in PHP, but is there something similar en python?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Do i have to work in this way?
    For example, in this code, I'm adding field6 if field2 has some value:
    Code:
    sum = 0
    for item in tuple: # tuple from a mysql fetched query
    	if item[2] == "Some value":
    		sum = sum + item[6]
    print sum
    Is this the only way to work with fetched results? because
    Code:
    while tuple:
      #do something
    loops for ever...
    So, I guess I got to change the while stuff with the good ol' for loop do i?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    well in python the while loop is a bit differant. well just working with objects like that in python is a bit differant. The objects don't automaticly change there state like they do in php. So you have to use a for loop to do this. so it isn't all that much harder i guess a bit new but not really. I personly like the python approch takes a bit to get use too but it isn't to hard.

    I do not beleave that the MySQLdb module has something like a fetch array in php. You could make a feature into it or on your own if you would like just query the database for the names and make a dictionary with the rows number as the key and the name is that the key points to. wouldn't be to hard.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Ok, I'll get used to it. Thanks.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    "select sum(fieldname) from table where otherfieldname == 'Some Value'"

    or

    Code:
    sum = 0
    for item in tuple: # tuple from a mysql fetched query
        name, company, stored_cash, fieldD = item
        if company == "Some value":
            sum += stored_cash
    
        print sum
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Ok sfb... thank you very much!

IMN logo majestic logo threadwatch logo seochat tools logo