vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Randomly pick one out of table (https://vborg.vbsupport.ru/showthread.php?t=74028)

AnhTuanCool 01-08-2005 11:08 PM

Randomly pick one out of table
 
So hi people,

I have a small question and very needed to be explained :)
I want to randomly pick an id out of a table so I wrote this
Code:

$allids = $DB_site->query("SELECT id FROM " . TABLE_PREFIX . "test");

$idarray = array();
while ($ids = $DB_site->fetch_array($allids))
{
$idarray[] .= $ids['id'];
}

$id = array_rand($idarray);

But it didn't work, I know my PHP and SQL skill still bad so I really need some support now. Thanks in advanced.

Creative Suite 01-08-2005 11:13 PM

$allids = $DB_site->query("SELECT id FROM " . TABLE_PREFIX . "test order by RAND() LIMIT 1");

filburt1 01-08-2005 11:21 PM

Almost...if you want to get just one randomly:
[sql]
SELECT column FROM table ORDER BY RAND() LIMIT 1
[/sql]
You should pretty much never have to get the entire contents of a table. It scales like crap and you'll cripple the server after the table gets too big. The exception is when you know the table is going to have very few rows and you need to read each and every one of the rows in the table.

AnhTuanCool 01-09-2005 02:13 AM

Got it worked right the first try, thank you very much guys :)


All times are GMT. The time now is 06:44 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.00968 seconds
  • Memory Usage 1,709KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (4)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete