Windows technical support
Podporni forum za uporabnike storitev spletnega gostovanja
This forum is part of windows webhosting service at www.hostmachine.net
 


Random zapis iz baze podatkov

To je neaktivna kopija originalnega foruma, prirejena za lazje indeksiranje z iskalnimi roboti.
Klikni tukaj za prehod na aktivni forum.
Click here to go to original active forum.


 
       Windows technical support Forum Index -> PHP
View previous topic :: View next topic  
Author Message
Pico



Joined: 18 Jan 2004
Posts: 250
Location: HostMachine.net

Posted: 3.12.2005, 23:05    Post subject: Random zapis iz baze podatkov  

Tlele sem naredu eno simpl funkcijo, ki ti iz baze potegne naključni zapis in ga izpiše. Funkcijo lahko shraniš kot v fajl, ki ga potem uporabiš kjerkoli. Recimo, da shraniš kot
random_sql.php

Kako uporabiti?
Čist simpl:
kjer bi rad vključu naključni tekst, vnesi tole: Code: <?php
        require("random_sql.php");
        echo PrintQuote("TABELA","JEZIK");
        ?>

Code: <?php
########################################################
# Let's assume TABLES are multilingual and named using the
# following rule:
#    table_language
# for example:
#    news_si
#    news_en
#    news_de
#
# Example:
# To ECHO random quote insice PHP generic page
#    echo PrintQuote("news","en");
# or in case we want to use random quote later, in FLASH applet:
#    $rnd_quote = PrintQuote("news","en");
#
########################################################
# Function:
########################################################

function PrintQuote($u,$l) {

// First, we reset random generator
        list($usec,$sec)=explode(" ",microtime());
        mt_srand($sec * $usec);

// Then we connect to server and select database to use
// HERE YOU MUST EDIT PROPER CONNECTION DATA!
        $dbhost="localhost";
        $dbuser="username";
        $dbpass="password";
        $dbase="ime_baze";
        $row_id="id"; // Name of ID row in table
        $row_text="text"; // NAme of TEXT row in table
        if(!@mysql_connect($dbhost,$dbuser,$dbpass)) exit("ERROR connecting to server <b>".$dbhost."</b>: ".mysql_error());
        if(!@mysql_select_db ($dbase)) exit("ERROR connecting to database <b> ".$dbase."</b>");
        // Here we format table name
        $dbtable=$u."_".$l;

// Now we count number of records in this table...
        $sqls="SELECT COUNT(".$row_id.") FROM $dbtable";
        $rows=@mysql_result(mysql_query($sqls),0);

// ...and select random record from table
        $sel_row=mt_rand(0,$rows);
        $sqls="SELECT ".$row_text." FROM $dbtable WHERE ".$row_id."=$sel_row";
        $out=@mysql_result(mysql_query($sqls),0);

// If there was an error, we output error
        if(mysql_error()!=0) $out=mysql_errno().mysql_error();
       
return $out;
}

########################################################
# End of function
########################################################
?>
Back to top  
 
       Windows technical support Forum Index -> PHP
Page 1 of 1


Te strani so generirane samo za lazje indexiranje z iskalnimi roboti.
Prosimo, da uporabite povezavo na glavni forum, kjer lahko aktivno sodelujete s svojimi prispevki