PDA

View Full Version : Query in database manipulation


sathishgforum
02-18-2004, 05:59 AM
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

Nightfire
02-18-2004, 10:19 AM
It's really a mysql question and should be moved there :)


<?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

sathishgforum
02-18-2004, 11:10 AM
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