Why this doesn't work(showing result of a request)?<

liunx

Guest
I found a tutorial on the net, but i have problem to make it work(maybe because it's not the same version of php). I added the line global $id; to declare the variable(but i'm not sure it's ok) and it's supposed to show the list of persons in the table the first time and when we click on one, it's supposed to show his details. The problem is that the details never show, but the id is written correctly in the adress.

Anyone have an idea of the problem?

P.S. i use php 4.3.4 and mySql 4.0.18


<html>
<body>
<?php

global $id;

$db = mysql_connect("localhost", "Sam");
mysql_select_db("mydb",$db);

// display individual record
if ($id) {

$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=http://www.htmlforums.com/archive/index.php/\"%s?id=%s\">%s %s</a><br>\n", $_SERVER['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!";

}

}

?>
</body>
</html>Originally posted by SamKook
I found a tutorial on the net, but i have problem to make it work(maybe because it's not the same version of php). I added the line global $id; to declare the variable(but i'm not sure it's ok) and it's supposed to show the list of persons in the table the first time and when we click on one, it's supposed to show his details. The problem is that the details never show, but the id is written correctly in the adress.

Anyone have an idea of the problem?

P.S. i use php 4.3.4 and mySql 4.0.18


<html>
<body>
<?php

global $id;

$db = mysql_connect("localhost", "Sam");
mysql_select_db("mydb",$db);

// display individual record
if ($id) {

$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=http://www.htmlforums.com/archive/index.php/\"%s?id=%s\">%s %s</a><br>\n", $_SERVER['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!";

}

}

?>
</body>
</html>



in this case the version of php/mysql doesn't matter,but rather or not you have register_globals on in your php.ini does...either way...doing global $id; isn't nessary...try this:


<html>
<body>
<?php

$id = $_GET["id"]; /*get id from query string using the superglobal array $_GET,so named because it recieves it's paramaters from the GET request method..which is...the query string.*/
$db = mysql_connect("localhost", "Sam");
mysql_select_db("mydb",$db);

// display individual record
if ($id) {

$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=http://www.htmlforums.com/archive/index.php/\"%s?id=%s\">%s %s</a><br>\n", $_SERVER['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!";

}

}

?>Thanks, it's working great.

Just to be sure, I got this notice displaying :

Notice: Undefined index: id in D:
\Data_Melville\Web\Melville\test.php on line 5


is this only because the var have no value at the beginning or there are another reason? and should I care that it's showing this(I guess not).Originally posted by SamKook
Thanks, it's working great.

Just to be sure, I got this notice displaying :


is this only because the var have no value at the beginning or there are another reason? and should I care that it's showing this(I guess not).

mm...I normally don't have my errors set on E_ALL to show notices so I'm really not sure what it means.But since it is of error level Notice I wouldn't worry about it...those are just that...notices.Not errors per se.it means you didn't define $id before you used it.


if(isset($_GET["id"])){
$id = $_GET["id"];
}
/*get id from query string using the superglobal array $_GET,so named because it recieves it's paramaters from the GET request method..which is...the query string.*/Originally posted by scoutt
it means you didn't define $id before you used it.


if(isset($_GET["id"])){
$id = $_GET["id"];
}
/*get id from query string using the superglobal array $_GET,so named because it recieves it's paramaters from the GET request method..which is...the query string.*/


I thought that error was:


Notice: Undefined Variable: var in ....


it didn't click that they were the same O_oyup,

Notice: Undefined index: id in D:
\Data_Melville\Web\Melville\test.php on line 5


notices are just that, notices. they are just telling you that you used a variable that wasn't define in the code before you used it. and it is not teh define() that you are thinking of.

$_GET['id']='';

also fixes that error, but hardly used in that way.
 
Back
Top