Creating a Query from results of previous Query

Hi all, i’m a total newbie and i know very little about coding, but i’m beating my way through learning. I’m having a little problem working out how to do the following, as i probably don’t know the correct terminology. So basically i have a database loaded with Karaoke Album Details : fields are Artist, Title, Description, DiscNo, TrackNo.
I have managed to create a query to display DiscNo in a simple table like below …

What i would like to do is then have each of the DiscNo clickable to either expand on page or link to a popup to show all of the associated fields ie TrackNo, Artist etc … Any help or direction would be greatly appreciated.

[php]<?php

$dbhost = ‘localhost’;
$dbuser = ‘’;
$dbpass = '
’;

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’);

$dbname = ‘*******’;
mysql_select_db($dbname);

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf(“SELECT DISTINCT DiscNo FROM CDG ORDER BY DiscNo ASC”);

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ’ . mysql_error() . “\n”;
$message .= 'Whole query: ’ . $query;
die($message);
}

echo "

";

// Use result
// Attempting to print $result won’t allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result))
{
echo “

”;
echo “”;
echo “”;
}

echo “

Disc No
” . $row[‘DiscNo’] . “
”;

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);

?> [/php]

Display the DiscNO like this

NOTE: you have to replace how you display your DiscNO with my code
[php]
echo “

<a href=’?discno=”.$row[‘DiscNo’]."’>".$row[‘DiscNo’]."";
[/php]

the above link when clicked will attach the DishNo to the link for example

http://example.com?discno=1457

so what you do next is on the page where you gonna show a more detailed information get the discno and run a query fetching everything where the discno = discno

[php]
if (isset($_GET[‘discno’]))
{
//… you run the query here and display everything according to the DiscNo
}
[/php]

good luck

Thank you for your reply … but i am a little lost …
I got the first bit and now have my list of linked DiscNo … which all point towards http://example.com?discno=1457 or similar …

But obviously no page exists at that address do i need to physically create those pages as i have thousands of DiscNos to display … not sure where to go next …

Thank you again

yes you have to create the page where you are going displayed a full detailed information for instance your new file name is “detail.php”

you have to change
[php]
echo “

<a href=’?discno=”.$row[‘DiscNo’]."’>".$row[‘DiscNo’]."";
[/php]
to
[php]
echo “<a href='detail.php?discno=”.$row[‘DiscNo’]."’>".$row[‘DiscNo’]."";
[/php]

and on that “detail.php” put the following code to get DiscNo sent from the previous page
[php]
if (isset($_GET[‘discno’]))
{
//… you run the query here and display everything according to the DiscNo
}
[/php]

Thank you again, but i have not really got anywhere. My details.php looks like this

[php]

<?php if (isset($_GET['DiscNo'])) { $query = sprintf("SELECT * FROM CDG WHERE DiscNo=DiscNo"); $result = mysql_query($query); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } echo ""; while ($row = mysql_fetch_assoc($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Artist Title Manufacturer Disc No Track No
" . $row['Artist'] . "" . $row['Title'] . "" . $row['Description'] . "" . $row['DiscNo'] . "" . $row['TrackNo'] . "
"; mysql_free_result($result); ?>[/php]

But i am getting an internal server error …due to a syntax error on line 42 … As i said i really am a beginner.

Thank you again !

yes you should definitely get error because you have QUERY errors i will fix them for you and point what was wrong

fixed code ( but not tested)
[php]

<?php if (isset($_GET['discno'])) { $query = sprintf("SELECT * FROM CDG WHERE DiscNo='".$_GET['discno']."'"); $result = mysql_query($query) or die(mysql_error()); echo ""; while ($row = mysql_fetch_assoc($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Artist Title Manufacturer Disc No Track No
" . $row['Artist'] . "" . $row['Title'] . "" . $row['Description'] . "" . $row['DiscNo'] . "" . $row['TrackNo'] . "
"; mysql_free_result($result); ?>

[/php]

the query was wrong, also i removed some redundant code you had its just better to have or die(mysql_error()) than a whole statements

i DID NOT test any of the cde myself so let me know if worked.
Good luck

Thank you again for your help but unfortunately i am still getting a 500 Internal Server Error.
Error log is showing PHP Parse error: syntax error, unexpected $end in /home/mydomain/public_html/test/detail.php on line 34

I have no idea, but greatly appreciate your help.

I forgot to close the curly Bracket ‘}’
insert a } before ?> tag

Actually have a result now … i just needed to close the } after

[php]$query = sprintf(“SELECT * FROM CDG WHERE DiscNo=’”.$_GET[‘discno’]."’");[/php]

seems to have sorted the problem out … thank you ever so much !

I have many more things that i wish to achieve with this script, so i am sure i will make many more posts over the next weeks, but for now this one is SOLVED !!

Thanks again.

you are welcome

very glad that i could help

Mark SOLVED!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service