Web Analytics Made Easy -
StatCounter Query in database manipulation - CodingForum

Announcement

Collapse
No announcement yet.

Query in database manipulation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Query in database manipulation

    Hi Pals,

    Howz the day? Im a newbie to PHP but wants to learn quickly the baseline of the PHP-MYSQL
    programming because of time-frame.So if anyone who have better knowledge in PHP-MYSQL can
    change the following script from ASP to PHP-MYSQL.
    ---------------------------------------------------------
    ASP CODE

    set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open "sample"
    set rsprod = server.CreateObject("ADODB.Recordset")
    rsprod.Open "select prd_nam from tbl_prod_details where psta =1 order by pdat desc",objConn,1,2

    rsprod.MoveLast
    cnt = rsprod.RecordCount
    cnt1 = cnt
    rndMax = cnt

    If 6 < cnt Then
    cnt1 = 6
    End If

    str = ","
    str1 = ","
    Do Until cnt1 = 0
    Randomize
    RndNumber = Int(Rnd * rndMax)

    If (InStr(1, str1, "," & RndNumber & "," ) = 0) Then
    str1 = str1 & RndNumber & ","
    cnt1 = cnt1 - 1
    rsprod.MoveFirst
    rsprod.Move RndNumber
    str = str & rsprod("prd_nam") & ","
    End If

    Loop

    rsprod.Close
    Set rsprod = Nothing

    ----------------------------------------------------------
    Scenario is to get the random 6 names from the set of values in the database.Where <str> is
    the variable to store the random numbers with delimiter of , (eg., Pepsi,Coke,Cake)
    (Where Pepsi Coke Cake are stored in the database as a product name)

    Any help would be greatly appreciated in this issue.

    Cheers
    SATHISH
    Winners Never Quit
    Quitters Never Win

  • #2
    It's really a mysql question and should be moved there

    PHP Code:
    <?php
    // Connect to database
    $link mysql_connect("localhost","username","password") or die("Failed to connect to database");
    mysql_select_db("dbname",$link);
    // Do the query
    $sql "SELECT prd_nam FROM tbl_prod_details WHERE psta='1' ORDER BY RAND() LIMIT 6";
    $query mysql_query($sql);
    // loop through results and print to page
    while($row mysql_fetch_array($query)){
        echo 
    $row['prd_nam'].'<br>';
    }
    ?>
    That'll just get 6 random entries from the database, not sure if that's what you're after as I'm still sleepy

    Comment


    • #3
      Hi NightFire!

      Thanks.Thats what i really looked for.
      A single peice of code suppress the multi line of asp code.Really WOW.

      Cheers
      SATHISH
      Winners Never Quit
      Quitters Never Win

      Comment

      Working...
      X