Hi I'm a PHP newbie, and am following a tutorial on webmonkey. If you can't be bothered to read all of it, the bit I'm having a problem with is this:
PHP does a nifty thing when it sees a name=value pair in the querystring. It automatically creates a variable with the name and value the querystring indicated. This feature allows us to test if it's the first or second time through this page. All we have to do is ask PHP if the variable $id exists.
And then as an example it gives this code:
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
// display individual record
if ($id) { // <-- this line
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
printf("First name: %s\n<br>", $myrow["first"]);
printf("Last name: %s\n<br>", $myrow["last"]);
printf("Address: %s\n<br>", $myrow["address"]);
printf("Position: %s\n<br>", $myrow["position"]);
} else {
// show employee list
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
do {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>
The thing is, the if ($id) line always skips to the else bit, i.e. when I click a user it stays as a list regardless of the "?id=1" at the end of the URL.
I did a google search, some people said about changing it to if (isset($id)), and others said to put the line
$id = $_GET['id']
before it. Neither of these methods work.
Please help! 😕 I'm using PHP 4.2.3 and mySQL 3.23.55 and running on IIS