Author Topic: Sin's ADODB questions  (Read 795 times)

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Sin's ADODB questions
« on: May 25, 2007, 01:41:14 PM »
Okay, I'm starting to use adodb and need some help getting into it.

First off:

how the fuck can I make it so that when I want a query returned in an associated array it's not stored in a 2-dimensional array?
here's my code so far:
Code: [Select]
$sql = "SELECT ID FROM items";
$rs = $db->Execute($sql);
$rs = $db->GetRows();

I used $db->SetFetchMode(ADODB_FETCH_ASSOC); before so no I get the result

Code: [Select]
Array
(
    [0] => Array
        (
            [ID] => 1
        )

    [1] => Array
        (
            [ID] => 2
        )

    [2] => Array
        (
            [ID] => 3
        )

    [3] => Array
        (
            [ID] => 4
        )

    [4] => Array
        (
            [ID] => 5
        )
)

but I simply need an array with only the IDs stored in it.

Either you can help me with the above question or then: how would I easily search a mutlidimensional array?

[?∂??]
actually here's what I want:
this query:
Code: [Select]
$sql = "SELECT * FROM items";

And now I want an array that is mutlidimensional, but instead of an int key I want the name as the key
Code: [Select]
[code]
Array
(
    [item1] => Array
        (
            [ID] => 1
            [name]=>item1
            [effect]=>5
        )
        ....
)
[/code]
« Last Edit: May 25, 2007, 02:01:39 PM by Sinzygy »

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Sin's ADODB questions
« Reply #1 on: May 25, 2007, 02:09:06 PM »
Umm, I'm not sure what you mean. Can this be done in normal SQL queries?

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Sin's ADODB questions
« Reply #2 on: May 28, 2007, 07:19:12 PM »
First if you are looking for a way to search a multi-dimensional array why?
It's easier and less resource intensives if you search via a db query..

but if you really need to do this in code then the following is something you might need... ADODB does not do this by default so you have to build the array yourself...

Code: [Select]
$items = "";
$rs = $db->Execute("SELECT id, name, effect FROM items");
while( !$rs->EOF ) {
  $items[$rs->fields['name'] = array("id" => $rs->fields['id'], "name" => $rs->fields['name'], "effect" => $rs->fields['effect]);
  $rs->MoveNext();
}

Didn't run that though the debugger but you should be able to figure it out.. You now have an array with the key of name from the db.. If you want the id as the key then just change $items[$rs->fields['name'] to $items[$rs->fields['id']

Creating online addictions, one game at a time:

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Sin's ADODB questions
« Reply #3 on: May 29, 2007, 02:23:33 AM »
thanks a lot, codestryker :)

I didn't really know how to use while-loops with adodb, but this makes it really clear.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal