PDA

View Full Version : selecting an id to display


Tjobbe
February 21st, 2005, 00:41
im using atm:
[syntax"php"]$get = mysql_query("SELECT * FROM coname where ID = 1") or die("Mmm pie:" . mysql_error());
[/syntex]

to display id 1 onto a page, this is a comany record.

how do i get it so that on the page that a link is clicked, it selects an id from the database (any given number) and then displays the output on the same page?

i.e click here for company a (id 1)
company b (id 2)

etc..

Tjobbe
February 21st, 2005, 16:29
thats meant to display:

[syntax:278d76ef55="php"]$get = mysql_query("SELECT * FROM coname where ID = 1") or die("Mmm pie:" . mysql_error());
[/syntax:278d76ef55]

oops!

sonicgroup
February 21st, 2005, 20:45
The easy way would be to generate your links from the database in a loop and have them print the link as page.php?id={companyID}. Then you'd simply add a check at the top of the page to see whether or not the id in the URL is empty or not:

[syntax:df7c0adde4="php"]if (!empty($_GET['id'])) {
$coID = $_GET['id'];
} else {
$coID = '';
}[/syntax:df7c0adde4]

Then use a simple if/else to determine if $coID is empty or not to set up your query:

[syntax:df7c0adde4="php"]if (empty($coID)) {
echo 'Please select a company.';
} else {
$result = mysql_query('SELECT * FROM coname WHERE ID = '.$coID) or die("Query failed: " . mysql_error());
// other stuff here - i.e. display
}
[/syntax:df7c0adde4]

Tjobbe
February 21st, 2005, 20:50
excellent, precisely what I needed, thanks Dave.