newbie array question

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default newbie array question

    Hi All

    Im new to php and are getting a bit confused about the sybase_fetch_array function (i think that this is the same as mysql_fetch_array?).

    If i have a valid sql query that returns three the records 'john','jack' and 'mary' and i want to put that into an array where array[0]->john,array[1]->jack etc how do i do this? Im trying to use sybase_fetch_array as follows:

    $query = "select name from names (where age <'23'')";
    $numero= sybase_connect("database" , "user" , "password" )
    or die ("Unable to connect to database server");
    $result=sybase_query($query,$numero );
    $names=sybase_fetch_array($result);


    I then try and do something like print $names[0];

    but it doesnt work. Sorry if this is really basic any help would be greatly appreciated!



    ---------------------------------
    Want to chat instantly with your online friends?*Get the FREE Yahoo!Messenger
    Bob pilly Guest

  2. Similar Questions and Discussions

    1. Byte array to string and back - newbie question
      Hi, I am trying to implement DES algorithm as described in the Microsoft article at...
    2. [newbie]saving and reading array of associative array
      i'm looking for examples of saving to file and reading back an array of associative array, in a ruby like way. saying i have something like : ...
    3. newbie question. how to assign an array to a hash table?
      Hi, I have something like: $value = 'A' 'C' 'G'; and I would like store this value on a hash table. my @value = split(' ',$value); print...
    4. Last number in the array --- access problem --Newbie question
      Here is the complete problem: I have two files MAER_FILE and EVAL_FILE MAER_FILE contains ITR Max_error Avg_error Min_error...
    5. array access problem - newbie question
      #-------------code----------# open (MAER_FILE, "MAER_output.txt"); while (<MAER_FILE>) { chomp; my @maer_array = split(/\t/); print...
  3. #2

    Default Re: newbie array question

    try this:

    $query = "select name from names (where age <'23'')";
    $numero= sybase_connect("database" , "user" , "password" ) or die ("Unable
    to connect to database server");
    $result=sybase_query($query,$numero );
    while($temp=sybase_fetch_array($result)){
    $names[] = $temp[name];
    }

    mfg.
    harry wiens

    ""Bob pilly"" <bobpilly@yahoo.co.uk> schrieb im Newsbeitrag
    news:20030710142939.70314.qmail@web40505.mail.yaho o.com...
    > Hi All
    >
    > Im new to php and are getting a bit confused about the sybase_fetch_array
    function (i think that this is the same as mysql_fetch_array?).
    >
    > If i have a valid sql query that returns three the records 'john','jack'
    and 'mary' and i want to put that into an array where
    array[0]->john,array[1]->jack etc how do i do this? Im trying to use
    sybase_fetch_array as follows:
    >
    > $query = "select name from names (where age <'23'')";
    > $numero= sybase_connect("database" , "user" , "password" )
    > or die ("Unable to connect to database server");
    > $result=sybase_query($query,$numero );
    > $names=sybase_fetch_array($result);
    >
    >
    > I then try and do something like print $names[0];
    >
    > but it doesnt work. Sorry if this is really basic any help would be
    greatly appreciated!
    >
    >
    >
    > ---------------------------------
    > Want to chat instantly with your online friends? Get the FREE
    Yahoo!Messenger


    Harry Wiens Guest

  4. #3

    Default newbie array question

    After some looking around - and maybe entering just the wrong keywords in
    Google :) - a short question: is it possible to retrieve the index number
    of the element of an array, let say that there is an array $myArray =
    ("John", "Tom", "Dick","Harry","Peter")
    I am looking for a function that tells me that Harry is the fourth element.
    (I have tried in google things like "retrieve index array php" but without
    result)
    Thx on beforehand

    Martien van Wanrooij.


    Martien van Wanrooij Guest

  5. #4

    Default Re: newbie array question

    Martien van Wanrooij wrote:
    > After some looking around - and maybe entering just the wrong keywords in
    > Google :) - a short question: is it possible to retrieve the index number
    > of the element of an array, let say that there is an array $myArray =
    > ("John", "Tom", "Dick","Harry","Peter")
    > I am looking for a function that tells me that Harry is the fourth element.
    > (I have tried in google things like "retrieve index array php" but without
    > result)
    Here:
    <?php
    $myArray = array("John", "Tom", "Dick","Harry","Peter");
    print_r($myArray);
    var_dump(whereInArray('Harry',$myArray,$error));
    echo $error;

    function whereInArray($needle,$haystack, &$err){
    // first be sure we have an array
    if(!is_array($haystack)){
    $err='The haystack was not an array.';
    return FALSE;
    }

    for($i=0;$i<count($haystack);$i++)
    if($haystack[$i] == $needle)
    return $i;

    $err='Needle '.$needle.' not found in haystack.';
    return FALSE;
    }
    ?>
    --
    Justin Koivisto - [email]spam@koivi.com[/email]
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Justin Koivisto Guest

  6. #5

    Default Re: newbie array question

    Justin Koivisto, obviously a huge fan of Elton John, wrote:
    >
    > for($i=0;$i<count($haystack);$i++)
    > if($haystack[$i] == $needle)
    > return $i;
    Alternately:

    foreach($haystack as $key => $value)
    if($value==$needle)
    return $key;

    This also works for non-numeric keyed arrays.

    /joe
    --
    The router is retarded and bastardized. In Dramatech, the Bursar's Office's
    emo kid is cute. The disassembler is exhausting. Jess's 2x-error-correcting
    variable from nick black's apartment is faulty.
    Disco Plumber Guest

  7. #6

    Default Re: newbie array question


    On 10-Nov-2003, "Martien van Wanrooij" <info@martienvanwanrooij.nl> wrote:
    > After some looking around - and maybe entering just the wrong keywords in
    > Google :) - a short question: is it possible to retrieve the index number
    > of the element of an array, let say that there is an array $myArray =
    > ("John", "Tom", "Dick","Harry","Peter")
    > I am looking for a function that tells me that Harry is the fourth
    > element.
    > (I have tried in google things like "retrieve index array php" but
    > without
    > result)
    > Thx on beforehand
    $myFlippedArray = array_flip($myArray);
    echo "Harry is element $myFlippedArray[Harry] of myArray";

    --
    Tom Thackrey
    [url]www.creative-light.com[/url]
    tom (at) creative (dash) light (dot) com
    do NOT send email to [email]jamesbutler@willglen.net[/email] (it's reserved for spammers)
    Tom Thackrey Guest

  8. #7

    Default Re: newbie array question

    Martien van Wanrooij wrote on Monday 10 November 2003 12:59:
    > After some looking around - and maybe entering just the wrong keywords in
    > Google :) - a short question: is it possible to retrieve the index number
    > of the element of an array, let say that there is an array $myArray =
    > ("John", "Tom", "Dick","Harry","Peter")
    > I am looking for a function that tells me that Harry is the fourth
    > element.
    > (I have tried in google things like "retrieve index array php" but
    > without result)
    How about:
    [url]http://www.php.net/manual/en/function.array-search.php[/url]


    --
    Business Web Solutions
    ActiveLink, LLC
    [url]www.active-link.com/intranet/[/url]
    Zurab Davitiani Guest

  9. #8

    Default Re: newbie array question

    Disco Plumber wrote:
    > Justin Koivisto, obviously a huge fan of Elton John, wrote:
    >
    >> for($i=0;$i<count($haystack);$i++)
    >> if($haystack[$i] == $needle)
    >> return $i;
    >
    >
    > Alternately:
    >
    > foreach($haystack as $key => $value)
    > if($value==$needle)
    > return $key;
    >
    > This also works for non-numeric keyed arrays.
    Ya, surprized that I didn't catch then when I copied it out of the
    file... ;)

    --
    Justin Koivisto - [email]spam@koivi.com[/email]
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Justin Koivisto Guest

  10. #9

    Default Re: newbie array question

    Justin Koivisto wrote:
    > Martien van Wanrooij wrote:
    >
    >> After some looking around - and maybe entering just the wrong keywords in
    >> Google :) - a short question: is it possible to retrieve the index
    >> number
    >> of the element of an array, let say that there is an array $myArray =
    >> ("John", "Tom", "Dick","Harry","Peter")
    >> I am looking for a function that tells me that Harry is the fourth
    >> element.
    >> (I have tried in google things like "retrieve index array php" but
    >> without
    >> result)
    >
    >
    > Here:
    > <?php
    > $myArray = array("John", "Tom", "Dick","Harry","Peter");
    > print_r($myArray);
    > var_dump(whereInArray('Harry',$myArray,$error));
    > echo $error;
    >
    > function whereInArray($needle,$haystack, &$err){
    > // first be sure we have an array
    > if(!is_array($haystack)){
    > $err='The haystack was not an array.';
    > return FALSE;
    > }
    >
    > for($i=0;$i<count($haystack);$i++)
    > if($haystack[$i] == $needle)
    > return $i;
    >
    > $err='Needle '.$needle.' not found in haystack.';
    > return FALSE;
    > }
    > ?>
    Note, it is possible that a value of int(0) is returned. On errors,
    boolean(FALSE) is returned. A simple if(whereInArray($find,$ar)) will
    not work...

    You will either need to check via:

    $error='';
    if(whereInArray($find,$ar,$error) && empty($error))

    or

    if(whereInArray($find,$ar,$error)!==FALSE)

    (I forgot to metioon that in the last message as well.)

    --
    Justin Koivisto - [email]spam@koivi.com[/email]
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Justin Koivisto Guest

  11. #10

    Default Re: newbie array question


    "Zurab Davitiani" <agt@mindless.com> schreef in bericht
    news:TvTrb.5411$Qi3.3366@newssvr29.news.prodigy.co m...
    > How about:
    > [url]http://www.php.net/manual/en/function.array-search.php[/url]
    Thank you Zurab,

    I have been actually looking at that page but I couldn't deduce from the
    code this particular problem of the "how-manieth" place a certain element
    took in the array.

    Martien.


    Martien van Wanrooij Guest

  12. #11

    Default Re: newbie array question


    "Justin Koivisto" <spam@koivi.com> schreef in bericht
    news:UdTrb.770$Uz.22362@news7.onvoy.net...
    > Here:
    > <?php
    > $myArray = array("John", "Tom", "Dick","Harry","Peter");
    > print_r($myArray);
    > var_dump(whereInArray('Harry',$myArray,$error));
    > echo $error;
    >
    > function whereInArray($needle,$haystack, &$err){
    > // first be sure we have an array
    > if(!is_array($haystack)){
    > $err='The haystack was not an array.';
    > return FALSE;
    > }
    >
    > for($i=0;$i<count($haystack);$i++)
    > if($haystack[$i] == $needle)
    > return $i;
    >
    > $err='Needle '.$needle.' not found in haystack.';
    > return FALSE;
    > }
    > ?>
    I thought there would be some standard functie to solve my problem but
    obviously there isn't . Thanks for your help

    Martien.


    Martien van Wanrooij Guest

  13. #12

    Default Re: newbie array question

    Martien van Wanrooij wrote on Tuesday 11 November 2003 14:12:
    >> How about:
    >> [url]http://www.php.net/manual/en/function.array-search.php[/url]
    >
    > Thank you Zurab,
    >
    > I have been actually looking at that page but I couldn't deduce from the
    > code this particular problem of the "how-manieth" place a certain element
    > took in the array.
    It returns the array key. If you take your original example:

    $myArray = ("John", "Tom", "Dick","Harry","Peter")
    $key = array_search("Harry", $myArray);

    The key should also be the position of the element in the array. This
    obviously doesn't take into account if you unset element(s) from the middle
    of the array, or if you have an associative array keyed on anything else
    than the position.

    If your real scenario includes other factors such as above, then do
    something like:

    $key = array_search(array_search("Harry", $myArray), array_keys($myArray));

    Untested, but something like that. :)

    --
    Business Web Solutions
    ActiveLink, LLC
    [url]www.active-link.com/intranet/[/url]
    Zurab Davitiani Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139