SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Help Please

  1. #1
    I am The Best Idiot. Period.
    Join Date
    Feb 2004
    Location
    Massachusetts.
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Please

    I want to be able to get data from a mysql database and then have it put in an array, which I will then use the rand() function. I also need it to automatically increase or decrease the options of randomizing{example increase from 3 to 4 in this rand(1,3)}, depending on how manys rows are in the table. I have spent about an hour trying to do this with no luck, so i hope you can help.

  2. #2
    Non-Member hyperimage's Avatar
    Join Date
    Dec 2003
    Location
    Croatia
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can we see your script?

  3. #3
    I am The Best Idiot. Period.
    Join Date
    Feb 2004
    Location
    Massachusetts.
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing that the script I have is connect to a mysql database and nothing else, after trying and trying to figure it out I have had no luck, so thats y I had posted here.

  4. #4
    Non-Member hyperimage's Avatar
    Join Date
    Dec 2003
    Location
    Croatia
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we cant give any response to u if we cannot see any structure!!!
    give us the html script and explain want u want connect to mysql and waht kind of output u want!!!

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like you want to pull a random record out of your database table. Try this SQL query
    SELECT * FROM table ORDER BY RAND() LIMIT 1

  6. #6
    I am The Best Idiot. Period.
    Join Date
    Feb 2004
    Location
    Massachusetts.
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let Me Clear this Up:

    What I would like is to have a random link feature on my website, which would be powered by php and mysql. What I need the script to do is take information from the database and insert it into an array FOR EXAMPLE: array('row 1','row 2'). where the rows 1&2 are rows of information from the database. For Example the First two random links in the database. The script needs to use two fields, name and url. and then take that information and form a link like so <a href="url goes here">name goes here</a>. So basically for each row of information in the database form a link like that and then insert it into an array where the rand() function then picks a link. The Only other problem is that that rand() function will need to increase or decrease it parameters when a row is either inserted or deleted.

  7. #7
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try my query, it will fetch you a random record out of the database table.
    PHP Code:
    // Database connection
    mysql_connect('localhost''root''') or die(mysql_error());
    mysql_select_db('database');

    // Fetch random record
    $sql 'SELECT * FROM links ORDER BY RAND() LIMIT 1';
    $result mysql_query($sql) or die(mysql_error());

    // Display link
    $row mysql_fetch_assoc($result);
    echo 
    '<a href="'.$row['url'].'">'.$row['name'].'</a>' 

  8. #8
    I am The Best Idiot. Period.
    Join Date
    Feb 2004
    Location
    Massachusetts.
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You very much, Gaheris


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
  •