#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    20
    Rep Power
    0
    Im relatively new to this, so I thank you in advance for your patience.

    All I need is to create an array with the results from a database column...

    Lanny
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    immortal,

    let us say ,you are issueing the following mysql query .

    $result = mysql_query ("SELECT * FROM tablename WHERE name='$name'");

    the row value from the above result can get it using mysql_fetch_array() function.

    $row = mysql_fetch_array($result)


    Now all the column values are in this $row variable.

    these column values you can split using following lines of code.

    do {
    print $row["name"];
    } while($row = mysql_fetch_array($result));




    ------------------

    SR -
    shiju.dreamcenter.net

    Web developer from GOD's own country!!!!
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    20
    Rep Power
    0
    I got that far, but the problem is, that I want to randomly pick one of the values. If i recall, i think the mysql_fetch_array gives a scalar array ?!?

    thanks,

    Lanny
  6. #4
  7. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    The random selection should be done in the SQL query, not after the array has been created.

    Here's an example:

    $query = "SELECT id,company,state,telephone,web_address,id*0+RAND() as rand_row FROM lenders WHERE states LIKE "%$contact_state%" ORDER BY rand_row LIMIT 3"; // Select 3 random lenders who serve contact's state.

    id*0+RAND uses the id of the record as the seed to generate random values, and LIMIT lets you choose as many or as few random rows as you want, then you can fetch the array as shown above.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    20
    Rep Power
    0
    Got it to work!

    <?php

    $db = mysql_connect("localhost","user","password");
    mysql_select_db("dbase",$db);
    $buzzword = "";

    for ($i=0; $i<4; $i++) {

    $word = mysql_query("SELECT word,word_number*0+RAND() as rand_row FROM buzzwords WHERE word_number=$i ORDER BY rand_row LIMIT 1", $db);

    ($myrow = mysql_fetch_array($word));

    $buzzword .= $myrow[word];
    $buzzword .= "n";

    }

    echo "$buzzword";

    ?>

    Thanks to all who contributed

Similar Threads

  1. Writing array of random numbers to a database
    By Beda in forum ASP Programming
    Replies: 2
    Last Post: February 2nd, 2004, 03:34 PM
  2. new to PHP-MySQL, deleting entries from database
    By gyppo100 in forum MySQL Help
    Replies: 4
    Last Post: February 1st, 2004, 11:37 AM
  3. Unique Entries in DataBase
    By BerettaXtreme in forum MySQL Help
    Replies: 1
    Last Post: January 7th, 2004, 05:05 PM
  4. delete all array entries that have value "xyz"
    By j0sh in forum PHP Development
    Replies: 10
    Last Post: January 7th, 2004, 04:41 PM
  5. Search MySQL database and filling an array with the values
    By FeelLikeGanesh in forum PHP Development
    Replies: 0
    Last Post: January 2nd, 2004, 05:51 PM

IMN logo majestic logo threadwatch logo seochat tools logo