Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

MYSQL on a page with "variabled url"

10:21 pm on Mar 27, 2012 (gmt 0)

New User

joined:Mar 24, 2012
votes: 0

Putting my other question aside ( [webmasterworld.com...] ), because i really want to learn something from this, i proceed with this:

I want to extract contents from a mysql table. I want that based on the url that the user goes to, the content changes.

table name: test
Columns: id, name, lastname
Row: 1234, My, Name

If the user goes to page.php?id=1234 I want the page do display Name: My
Last Name: Name

So basically, i want the script to get a column (in this case the ID column) and change the contents of the page, based on the part in the row that corresponds to that column. This is all done through the URL.

Im not sure if im explaining this enough so if you need any more information, please ask. In my other thread i was asking about a specific situation on my website. This one is more general because i want to learn, and then implement that to MY situation on my own.

Thanks in advance,

Pedro S Lopez

[edited by: eelixduppy at 12:20 pm (utc) on Mar 29, 2012]
[edit reason] linked url [/edit]

4:08 pm on Mar 28, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

I think the answer is still the same. :-) Using this example,

table name: test
Columns: id, name, lastname
Row: 1234, My, Name

and this URL,


you would do (again, typing on the fly -expect errors if you copy and paste)

if (isset($_GET['id']) and is_numeric($_GET['id']) and ($_GET['id'] > 0)) {
// You can use *, but specific field names are often more efficient
// One of the rare occasions raw input is OK because we've already verified it's a number
$query = "select `name`,`lastname` from `test` where id=".$_GET['id'];
$result = mysql_query($query) or die("Cannot execute $query");
if ($row = mysql_fetch_array($result)) {
// You may want these individually - see below
$fname = $row['name'];
$lname = $row['lastname'];
$output = "<p>$fname $lname</p>";
else {
$output = '<p>No results were found with that query.</p>';
else { $output = '<p>Invalid input supplied.</p>'; }

Now you can do whatever you want with "$fname,$lname,$output." Basic example,

<title><?php echo "$fname $name"; ?>'s page</title>
<h1>Hello <?php echo "$fname $lname"; ?></h1>
<?php echo $output; ?>

Once you get that sorted out you can start looking at URL rewriting, usually done on a Linux server with mod_rewrite, so instead of a query string ?id=1234 you can do


Which is far more memorable and SEO friendly.
10:46 pm on Mar 28, 2012 (gmt 0)

New User

joined:Mar 24, 2012
votes: 0

THANK YOU! After fixing some errors from that code you gave me and replacing some variables to adapt to my situation, it is working perfectly! I completely understood and will apply this to my project. Thank you so much for all your help.