RE: random rows...what about tables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



to get all the table names, then return a random table name, try the
following:

<?php

	for($i=0;$result;$i++)
		$table_names[$i] = $result[0];
	echo 'A random table name: '.array_rand($table_names);
?>

dont forget, when you use a mysql_query(), it returns a result set with more
than one value for each row returned. ie, $array[$i] could be anything from
the table name to its size to its whatever ...

Regards,
    Matthew Moldvan

---------------------------------------
 System Administrator
 Trilogy International, Inc
 http://www.trilogyintl.com/ecommerce/
---------------------------------------

-----Original Message-----
From: Hutchins, Richard [mailto:Richard.Hutchins@GetingeCastle.com]
Sent: Tuesday, December 17, 2002 9:26 AM
To: 'Bruce Levick'; php-db@lists.php.net
Subject: RE:  random rows...what about tables


I think it's because mysql_list_tables returns a resource identifier
($result). You have to iterate through the resource identifier ($result)
with a while() loop to get the actual table names out. From the PHP.NET
site:

<?php
    $dbname = 'mysql_dbname';

    if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
        print 'Could not connect to mysql';
        exit;
    }

    $result = mysql_list_tables($dbname);
    
    if (!$result) {
        print "DB Error, could not list tables\n";
        print 'MySQL Error: ' . mysql_error();
        exit;
    }
    
    while ($row = mysql_fetch_row($result)) {
        print "Table: $row[0]\n";
    }

    mysql_free_result($result);
?>

In the example above, I think the output would just be one table name.
However, you're going to have (and want) a list of multiple table names, if
I understand your problem correctly. In which case you'd have to put the
table names in an array called $tablenames so your array_rand($tablenames)
will work.

Haven't tested this out or done this specifically before, so I hope this
helps.
> -----Original Message-----
> From: Bruce Levick [mailto:bruce@vivamotion.com]
> Sent: Tuesday, December 17, 2002 9:19 AM
> To: php-db@lists.php.net
> Subject: Re:  random rows...what about tables
> 
> 
> Sorry,
> Have updated my script with a little help. Upon output i am 
> recieving this.
> 
> Warning: Argument to array_rand() has to be an array in
> C:\WORKING\portfolio\vivamotion\site\php\request.php on line 51
> 
> this is the code below.
> //////////////////////////////////////////////////////////////
> //////////////
> /////
> 
> <?php
> //listing table in database
> $alltables = mysql_list_tables("portfolio");
> 
> //random array
>  $randomtable = array_rand($alltables);
> 
>  $all = mysql_query("SELECT * FROM Illustrations");
>  $totalRows_Recordset1 = mysql_num_rows($all);
>  $rndm = mysql_query("SELECT * FROM Illustrations ORDER BY 
> RAND() LIMIT
> 4,$totalRows_Recordset1");
>  if (!$rndm) {
>       echo("<P>Error performing query: " .
>            mysql_error() . "</P>");
>       exit();
>     }
> 
>  $randrow = mysql_fetch_array($rndm);
>  ?>
> 
> //output to browser
> <?php
> print "$randomtable\n";
> ?>
> 
> 
> Hope that helps
> 
> 
> 
> 
> ----- Original Message -----
> From: "Jason Wong" <php-db@gremlins.biz>
> To: <php-db@lists.php.net>
> Sent: Tuesday, December 17, 2002 11:48 PM
> Subject: Re:  random rows...what about tables
> 
> 
> > On Tuesday 17 December 2002 21:38, Bruce Levick wrote:
> > > Bruce Levick - Vivamotion
> > > Been searching for an answer to selecting a random table 
> and then a
> random
> > > row within the selected table.
> > >
> > > I have this code which successfully selects a random row 
> within a hard
> > > coded table. But just can't get the random table working.
> >
> > [snip]
> >
> > > Anybody see the solution??
> >
> > First we need to know _what_ the problem is. I think we can 
> assume your
> code
> > doesn't work the way you expected. Can you tell us _how_ it 
> doesn't work?
> > Error messages?
> >
> > --
> > Jason Wong -> Gremlins Associates -> www.gremlins.biz
> > Open Source Software Systems Integrators
> > * Web Design & Hosting * Internet & Intranet Applications 
> Development *
> >
> >
> > /*
> > We place two copies of PEOPLE magazine in a DARK, HUMID mobile home.
> > 45 minutes later CYNDI LAUPER emerges wearing a BIRD CAGE 
> on her head!
> > */
> >
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> 
> 
> -- 
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux