SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Gathering Array from MySQL

    Hi,

    This would be my fist post on this board. And I hate it to be a question, but it is going to be anyway. What I want to do is check a table in a database, A table will have the following columns:

    id
    username
    credits
    status

    I want to gather all the "ids" that have the "status" of "1" and then leave them in a variable format of "1, 2, 3, 7..." depending on the numbers that have the status of 1. Just a note, the id is a #.

    Thank you, I hope someone can help me out.


    - Zoltrix

  2. #2
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT id
    FROM table
    WHERE status=1
    ORDER BY id

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, So now I have something like this

    PHP Code:
    $query "SELECT id FROM credits WHERE status=1 ORDER BY id";
    $run mysql_query($query); 
    How would I store it as a variable in the format of

    1, 2, 3, 7..." depending on the numbers

  4. #4
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $idList '';
    while (
    $row mysql_fetch_assoc($run)) {
       if (
    strlen($idList) > 0) {
          
    $idList .= ', ';
       }
       
    $idList .= $row['id'];

    Edit: Semicolon's are usually important.
    Last edited by cyngon; Jun 19, 2002 at 19:33.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,

    I have this now,
    PHP Code:
    $query "SELECT id FROM credits WHERE status=1 ORDER BY id";
    $run mysql_query($query);
    $idList '';
    while (
    $row mysql_fetch_assoc($run)) {
       if (
    strlen($idList) > 0)  {
          
    $idList .= ', '
       
    }
       
    $idList .= $row['id'];

    and Im getting the idiot error "Parse error: parse error in C:\apache\htdocs\index2.php on line 39"

    Line 39 is " }" just below " $idList .= ', '"

    any ideas?

  6. #6
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You forget the semicolon. Should be
    PHP Code:
    $idList .= ', '

  7. #7
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good catch. Thanks.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thank you for that, Works perfectly,


    But I have a new problem, My current code looks like this"

    PHP Code:
    $query "SELECT id FROM credits WHERE status=1 ORDER BY id";
    $run mysql_query($query);
    $idList '';
    while (
    $rw mysql_fetch_assoc($run)) {
       if (
    strlen($idList) > 0)  {
            
    $idList .= ', ';
       }
       
    $idList .= $row['id'];
    }

    echo 
    "$idList<br>\n";

    srand ((float) microtime() * 10000000);
    $input = array ("Number2""Number3""Number 2");
    $rand_keys array_rand ($input2);
    print 
    $input[$rand_keys[0]]."\n";
    print 
    $input[$rand_keys[1]]."\n"
    The line that reads

    PHP Code:
    $input = array ("Number2""Number3""Number 2"); 
    is were I am having problems. I would like to have the variable "$idList" Work in that line. But when I use it, I get this erro
    Warning: Second argument to array_rand() has to be between 1 and the number of elements in the array in C:\apache\htdocs\index2.php on line 47
    Line 47 is
    PHP Code:
    $rand_keys array_rand ($input2); 
    So the last few lines are looking like
    PHP Code:
    srand ((float) microtime() * 10000000);
    $input = array ($idList);
    $rand_keys array_rand ($input2);
    print 
    $input[$rand_keys[0]]."\n";
    print 
    $input[$rand_keys[1]]."\n"

    What am I doing wrong?

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

  10. #10
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you want to get the id in an array or in a string (comma-separated)? Because cyngon's code will create a string of comma-separated id ("1, 2, 3, ..."). Reading your last post, seems that you want to store the id in an array and select 2 values randomly. Did I get it right? In that case, the code will be something like this
    PHP Code:
    $query "SELECT id FROM credits WHERE status=1 ORDER BY id";
    $run mysql_query($query);
    $idList = array();
    while (
    $rw mysql_fetch_assoc($run)) {
       
    // add each id to $idList
       
    $idList[] = $row['id'];
    }

    print_r($idList);
    echo 
    "<br>\n";

    srand ((float) microtime() * 10000000);
    // since $idList is already an array, we can use it directly here
    $rand_keys array_rand ($idList2);
    print 
    $input[$rand_keys[0]]."\n";
    print 
    $input[$rand_keys[1]]."\n"

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, The code your suplied print's out
    Array ( [0] => [1] => [2] => [3] => [4] => [5] => )
    Dont want that, Want it to give me 2 random numbers out of the list of id's

    I think I need an array that will make the line


    PHP Code:
    $rand_keys array_rand ($idList2); 
    In theroy look like

    PHP Code:
    $input = array ("1""2""3");
    $rand_keys array_rand ($input2); 
    So I think I do need an array, but it must be in the format of
    PHP Code:
    array ("1""2""3"); 

  12. #12
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "SELECT id FROM credits WHERE status=1 ORDER BY id";
    $run mysql_query($query);
    $row mysql_fetch_row ($run);

    print_r($run);
    echo 
    "<br>\n";

    srand ((float) microtime() * 10000000);
    $rand_keys array_rand ($run2);
    print 
    $input[$rand_keys[0]]."\n";
    print 
    $input[$rand_keys[1]]."\n"
    I've never used mysql_fetch_row() before and i haven't tested this so I have no idea if it will work.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  13. #13
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    << Edit: I think the post's timestamp is wrong. My post doesn't show up as the last one in the thread. Maybe it's the new server's setting?? >>
    Last edited by oodie; Jun 20, 2002 at 17:16.

  14. #14
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nada,


  15. #15
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should be
    PHP Code:
    while ($row mysql_fetch_assoc($run)) { 
    and not
    PHP Code:
    while ($rw mysql_fetch_assoc($run)) { 

  16. #16
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just saying nada doesn't help me if i want to know what i did wrong so i can fix it for you. what was the output?
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  17. #17
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $query "SELECT id FROM credits WHERE status=1 ORDER BY id";
    $run mysql_query($query);
    $idList = array();
    while (
    $rw mysql_fetch_assoc($run)) {
       
    // add each id to $idList
       
    $idList[] = $row['id'];
    }

    print_r($idList);
    echo 
    "<br>\n";

    srand ((float) microtime() * 10000000);
    // since $idList is already an array, we can use it directly here
    $rand_keys array_rand ($idList2);
    print 
    $idList[$rand_keys[0]]."\n";
    print 
    $idList[$rand_keys[1]]."\n"

  18. #18
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,

    Thank you everyone, I have taken everyones coding and edited were needed.

    I have now come up with
    PHP Code:

    $query 
    "SELECT id FROM credits WHERE status=1 ORDER BY id";
    $run mysql_query($query);
    $idList = array();
    while (
    $row mysql_fetch_assoc($run)) {
       
    // add each id to $idList
       
    $idList[] = $row['id'];
    }


    srand ((float) microtime() * 10000000);
    // since $idList is already an array, we can use it directly here
    $rand_keys array_rand ($idList2);
    print 
    $idList[$rand_keys[0]]."\n";
    print 
    $idList[$rand_keys[1]]."\n"
    its working like a charm...

    Thank you again,

  19. #19
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thats weird. oodie's last two posts weren't there when i posted last time. is the server messed up or something?
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor


Bookmarks

Posting Permissions

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