Page 1 of 1
variable not being picked up from url
Posted: Mon Aug 19, 2002 11:03 am
by Patriot
help!
heres exactly what my 2.php script is:
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?
Posted: Mon Aug 19, 2002 11:06 am
by llimllib
Posted: Mon Aug 19, 2002 11:06 am
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
Posted: Mon Aug 19, 2002 11:51 am
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.
Posted: Mon Aug 19, 2002 12:00 pm
by twigletmac
Try
Code: Select all
$sql = "SELECT * FROM articles WHERE user_id=".$_GETї'id'];
$result = mysql_query($sql) or die(mysql_error());
instead of
Code: Select all
$result = mysql_query("SELECT * FROM articles WHERE user_id=$_GETї'id']", $db);
Mac
Posted: Mon Aug 19, 2002 12:48 pm
by Patriot
wow, cool! you got it right on the dot.
thanks tons!
Posted: Tue Aug 20, 2002 4:17 am
by gratou
you can also try
Code: Select all
$sql = "SELECT * FROM articles WHERE user_id={$_GETї'id']}";
Posted: Tue Aug 20, 2002 4:29 am
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.