PDA

View Full Version : Need help on script that displays multiple MySQL rows


B&WDude
Sun 27th May '01, 9:48pm
I've started creating a script that accesses the database and gets the information, but I need help getting it to display the right results. It displays the right number, just not the right data. Here is what I have so far (using a for loop):

// define server variables
$DBServer = "server";
$DBUser = "user";
$DBPass = "pass";
$DBName = "database";
$DBTable = "songs";

// connect to MySQL database
mysql_connect($DBServer,$DBUser,$DBPass) or die("Unable to connect to database.");
mysql_select_db("$DBName") or die("Unable to access the song list.");

// get number of songs for while loop
$sqlquery = "SELECT songid FROM songs WHERE songid > 0 ORDER by songid DESC LIMIT 0,10";
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);
$lastquery = "SELECT songid FROM songs WHERE songid = $number";
$lastresult = mysql_query($lastquery);
$last = mysql_result($lastresult, "songid");

for ($counter=1; $counter<=$last; $counter++) {

// get song numbers
$songquery = "SELECT songid FROM $DBTable ORDER by songid LIMIT 0,10";
$songresult = mysql_query($songquery);
$song = mysql_result($songresult,"songid");


// get song titles
$titlequery = "SELECT title FROM $DBTable ORDER by songid LIMIT 0,10";
$titleresult = mysql_query($titlequery);
$title = mysql_result($titleresult,"title");

// get download link
$dloadquery = "SELECT dloadlink FROM $DBTable ORDER by songid LIMIT 0,10";
$dloadresult = mysql_query($dloadquery);
$dload = mysql_result($dloadresult,"dloadlink");

// print the song info out
print "<B>$song. <a href=$dload>$title</a>";
}


The script currently gives me the same row of data twice (2 rows in the database total). I want it to display one of each row.

What shoul I do?
Thanks in advance!

krs-one
Sun 17th Jun '01, 1:57pm
you have this:

for ($counter=1; $counter<=$last; $counter++) {


Change it to this:

for ($counter=0; $counter<$last; $counter++) {


Since the number of rows is storred in an array (I believe), it starts at zero, not one.

Hope this helps.
-Vic