variable not being picked up from url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

variable not being picked up from url

Post by Patriot »

help!
heres exactly what my 2.php script is:

Code: Select all

<?
echo "$id"
?>

but when i put
localhost/2.php?id=hello

it says its undefined!

i dont know if i have register_globals on or off. could this be a problem? should i check?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

most likely register_globals is off, so use $_GET['id'] instead

echo (int)ini_get("register_globals"); // shows the value of register_globals
Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

Post by Patriot »

ok, thank you.
that script works fine now.

but the really reason i was asking this is:

<html>
<body>
<?php $db = mysql_connect("localhost", "target", "root");
mysql_select_db("news",$db);
$result = mysql_query("SELECT * FROM articles WHERE user_id=$_GET['id']",$db);
printf("Article name: %s<br>\n", mysql_result($result,0,"article_name"));
?>
</body>
</html>



this script.

now i tried what you said, and i did read that article, but i guess i'm just not a quick learner.

i get this:


Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\apache\htdocs\1.php on line 5


i'll keep researching, but if someone could help i would appreciate it.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try

Code: Select all

$sql = "SELECT * FROM articles WHERE user_id=".$_GET&#1111;'id'];
$result = mysql_query($sql) or die(mysql_error());
instead of

Code: Select all

$result = mysql_query("SELECT * FROM articles WHERE user_id=$_GET&#1111;'id']", $db);
Mac
Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

Post by Patriot »

wow, cool! you got it right on the dot.
thanks tons!
gratou
Forum Newbie
Posts: 10
Joined: Sun Jul 28, 2002 4:27 am
Location: Auckland, NZ

Post by gratou »

you can also try

Code: Select all

$sql = "SELECT * FROM articles WHERE user_id=&#123;$_GET&#1111;'id']&#125;";
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

or assign $_GET['id'] to a variable

$id = $_GET['id'];

then use that in your SQL statement

$sql = "select * from articles where user_id = $id";

bit longer to code but I think it improves the reading of the code, particularly if you have a large SQL statement. Good habit to get into.
Post Reply