homepage Welcome to WebmasterWorld Guest from 54.226.80.55
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
MYSQL on a page with "variabled url"
psl101




msg:4434159
 10:21 pm on Mar 27, 2012 (gmt 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.

Example:
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]

 

rocknbil




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

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

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

and this URL,

page.php?id=1234

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


$output=$fname=$lname=$null;
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,

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

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

sitname.com/Firstname-Lastname

Which is far more memorable and SEO friendly.

psl101




msg:4434612
 10:46 pm on Mar 28, 2012 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved