Jump to content

Something not working with my variables.


Shipton

Recommended Posts

Alright so I've been going through the PHP/MySQL tutorial on webmonkey (http://www.webmonkey.com/webmonkey/99/21/index3a_page3.html?tw=programming), but no matter what, it doesn't detect an 'id' variable. I've even tried passing variables of other names. It works in the URL, but it never does 'what it's supposed to' - i.e., it just keeps showing the list of names rather than that specific name.

Link to comment
Share on other sites

Actually I did, the URL listed in the first post contains the entire script.

 

Here it is, again:

 

<html>
<body>
<?php
$db = mysql_connect("localhost", "root", "god");

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=\"%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!";

    }

}


?>



</body>



</html>

Link to comment
Share on other sites

http://www.webmonkey.com/webmonkey/99/21/index3a_page3.html?tw=programming

 

if this is your webaddress going to the script you have provided, there is no id in this URL. This would be $_GET['tw'] and you would have to change "id" in your SELECT statement to your DB

 

you could do http://www.webmonkey.com/webmonkey/99/21/index3a_page3.html?tw=programming&id=

 

but you would have to pass the id through the URL the same way you passed "programming" through

Link to comment
Share on other sites

You're trying to use register_globals, you need to define $id before using it.

$id = $_GET['id'];

 

That got it, thanks very much.

 

Thanks also to everyone who responded!

 

If I have one last question , it would be whether it matters if I use single or double quotes; I see some snippets with either, so I take it it doesn't?

Link to comment
Share on other sites

That actually clears a lot up, thanks.

 

Alright, yet another newb question spawning from that ancient PHP tutorial: when I do what the guy that wrote it says and give it the extension php3 instead of just  php, instead of the expected results, it gives me this:

 

", $myrow["first"]); printf("Last name: %s\n

", $myrow["last"]); printf("Address: %s\n

", $myrow["address"]); printf("Position: %s\n

", $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("%s %s

\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!"; } } ?>

 

Why is that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.