-Karl- Posted July 6, 2009 Share Posted July 6, 2009 Well, I have a database with guides on it, sorted by their ID, which is auto increment and the primary key. What I'm trying to do is have a page called guides.php, this will show a table containing the guide names with links to the guides. The links will be guides.php?id=1 ID number according to the ID in the database. The guide page will display the information from the database. Example: If a guide is called PHP - Beginner and has an ID on the MySQL database of 5, then I would go to guides.php and click "PHP - Beginner" on the list, this would then take me to guides.php?id=5, this page would show me the guide information from the other fields in the database, instead of the table. Look at this page to understand what I mean more: http://www.zybez.net/quests.php I just have no idea where to begin as I've never tried to do it before. Any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/ Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 Assuming you already have the database setup... here's the php query. <?php if(isset($_GET['id'])){ $id = mysql_real_escape_string($_GET['id']); $query = "SELECT * FROM `table` WHERE `id` = '$id'"; $run = mysql_query($query); if($run){ $arr = mysql_fetch_assoc($run); }else{ die("Unable to connect to database."); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-869981 Share on other sites More sharing options...
-Karl- Posted July 6, 2009 Author Share Posted July 6, 2009 Yes it is already set up, but then how would I make it do what I want it to do? Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-869985 Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 The code I provided above would allow the url with the $_GET variable to work. To generate the page that displays the urls with the $_GET vars contained would be something like this <?php // Collect guides $query = "SELECT `id`,`guide` FROM `guides`"; $run = mysql_query($query); if($run){ while($arr = mysql_fetch_assoc($run)){ echo <<<HTML <a href="guides.php?id={$arr['id']}">{$arr['guide']}</a><br /> HTML; } }else{ die("Unable to connect to database."); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-869989 Share on other sites More sharing options...
-Karl- Posted July 6, 2009 Author Share Posted July 6, 2009 Ahh okay, so that would create the table which I want on quests.php, but how about the actual guides on quests.php?id=1 Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-869994 Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 That would be explained in my first post... I'll reiterate in further detail below. <?php // assuming guide.php?id=2 if(isset($_GET['id'])){ // first grab the id $id = mysql_real_escape_string($_GET['id']); // second query for that id $query = "SELECT `guide` FROM `table` WHERE `id` = '$id'"; $run = mysql_query($query); if($run){ // third display the guide $arr = mysql_fetch_assoc($run); echo $arr['guide']; }else{ die("Unable to connect to database."); } } ?> Does this answer your question? Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-869995 Share on other sites More sharing options...
-Karl- Posted July 6, 2009 Author Share Posted July 6, 2009 Sorry, I must be a pain, but it doesn't display anything on guides.php now. However, guides.php?id=1 works. Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-870000 Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 Yeah, right now I haven't written a fail-safe in case an id wasn't provided. I'd probably redirect the user back to the listing (sample below). <?php // assuming guide.php?id=2 if(isset($_GET['id'])){ // first grab the id $id = mysql_real_escape_string($_GET['id']); // second query for that id $query = "SELECT `guide` FROM `table` WHERE `id` = '$id'"; $run = mysql_query($query); if($run){ // third display the guide $arr = mysql_fetch_assoc($run); echo $arr['guide']; }else{ die("Unable to connect to database."); } }else{ // if the headers haven't been set yet you can use header("Location: listpage.php"); // otherwise use js echo <<<SCRIPT <script type="text/javascript"> window.location = 'listpage.php'; </script> SCRIPT; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-870002 Share on other sites More sharing options...
-Karl- Posted July 6, 2009 Author Share Posted July 6, 2009 Oh, so I'd have to use a seperate page for the list, rather than have it on quests.php? Because on www.zybez.net/quests.php, they've managed it. I have no clue how. Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-870003 Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 Ahh I see what you're saying. If a guide id isn't specified, you want to list them all. The script below should do the trick. <?php // assuming guide.php?id=2 if(isset($_GET['id'])){ // first grab the id $id = mysql_real_escape_string($_GET['id']); // second query for that id $query = "SELECT `guide` FROM `table` WHERE `id` = '$id'"; $run = mysql_query($query); if($run){ // third display the guide $arr = mysql_fetch_assoc($run); echo $arr['guide']; }else{ die("Unable to connect to database."); } }else{ // Collect guides $query = "SELECT `id`,`guide` FROM `guides`"; $run = mysql_query($query); if($run){ while($arr = mysql_fetch_assoc($run)){ echo <<<HTML <a href="guides.php?id={$arr['id']}">{$arr['guide']}</a><br /> HTML; } }else{ die("Unable to connect to database."); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-870004 Share on other sites More sharing options...
-Karl- Posted July 6, 2009 Author Share Posted July 6, 2009 That's absolutely perfect! Thanks a lot for helping me out Quote Link to comment https://forums.phpfreaks.com/topic/164992-solved-help-with-the-_get-variable-with-mysql/#findComment-870007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.