Jump to content

passing variables in url


btd117

Recommended Posts

I am trying to set up a page like this:

ex:  /index.php?id=2

and was given this code by someone here, yet it still yields a blank page...

<?php

include 'admin/global.php';

$result = mysql_query("SELECT * FROM ncaa ORDER BY id WHERE id='{$_GET['id']}'");

while($row=mysql_fetch_array($result)) {

  print '<p>' .$row['id']. '</p>';
 
}

?>

When I remove the WHERE id='{$_GET['id']}', it queries the table results correctly. Do i have a problem with my dB? The table has 3 entries in there for sure with ID set to auto_increment, primary key, and unique.
Link to comment
https://forums.phpfreaks.com/topic/27834-passing-variables-in-url/
Share on other sites

i'm not tottally sure but doesn't the WHERE clause have to come before the ORDER? and i would add an if statement like this:

[code=php:0]
if(!isset($_GET['id'])){ $id = 1; }else{ $id = $_GET['id']; }
[/code]
that, obviously, sets a variable ID with the value of 1 if they didnt specify it in the url, otherwise it sets ID as the one in the url.

then change your mysql query to this:
[code=php:0]
$result = mysql_query("SELECT * FROM `ncaa` WHERE `id`='{$id}' ORDER BY `id` DESC");
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.