Gostovanje na Windows strežnikih, ponudba webhosting, ASP.NET, PHP, MySQL


Windows technical support
Podporni forum za uporabnike storitev spletnega gostovanja
Reply to topic
Random zapis iz baze podatkov
Pico
Site Admin

Joined: 18 Jan 2004
Posts: 234
Location: HostMachine.net
Reply with quote
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
########################################################
?>

_________________
Site admin alias Labsy
Vsi nasveti in tehnične rešitve so podani v dobri veri in za ljudi z razčiščenimi pojmi o veljavni zakonodaji.
Odgovornost prevzemam izključno in samo za tiste posege, ki jih opravim lastnoročno.
View user's profileSend private messageVisit poster's websiteMSN Messenger
Random zapis iz baze podatkov
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT + 1 Hour  
Page 1 of 1  

  
  
 Reply to topic