faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

88 of 113 people (78%) answered Yes
Recently 8 of 10 people (80%) answered Yes


about "php3?id=xxxxx" OUTPUT RESULT TO HTML ...
In MySQL I have a primary key defined as a BIGINT. From a PHP page I pass the primary key of a recor

Mar 15th, 2008 21:30
dman, ha mo, Philip Olson, John Leung, Jenny Grotelueschen, http://www.ttnr.org

Most likely you'e wanting to specify a result from a database so we'll  
go on this assumption and will demonstrate using MySQL :
  // Get the GET variable from the url.  Most likely $id will be
  // available (if register_globals setting is on in php.ini) but 
  // we'll go through $HTTP_GET_VARS today.
  $id = $HTTP_GET_VARS['id'];
  // First, check to make sure $id is numeric ... just in case :
  if (is_numeric($id)) {
    $conn   = mysql_connect($db_host,$db_user,$db_pass); 
    $db     = mysql_select_db($db_name, $conn); 
    // Create a query and pay special attention to the 
    // WHERE id = '$id' as it selects only the data with 
    // this given id which is what is wanted here
    $q = "SELECT id,name,email FROM $db_table WHERE id='$id'";
    $r = mysql_query($q); 
    // Now that we have the result ($r) which you can play with, 
    // here is one of a million ways to do so :
      print '<table>';
        while ($row = mysql_fetch_array($r)) {
           print "<tr>\n";
           print '<td>'. $row['id']    ."</td>\n";
           print '<td>'. $row['name']  ."</td>\n";
           print '<td>'. $row['email'] ."</td>\n";
           print "</tr>\n";
      print '</table>';
  } else {
      $errors[] = '$id is not numeric, please make it so';
Now assuming the above code is in foo.php, example.com/foo.php?id=33 
will print the id,name and email address of id number 33 from the 
database.  If you wanted to loop through and display all id's, simply 
take off the WHERE clause and print the database information 
accordingly.  Eventually you may have something like the following 
within your loop :
  echo '<a href="edit.php?id='. $row['id'] .'">'. $row['name'] .'</a>';
And edit.php would take the id, put the information within a form and 
allow one to edit the information.
If you're just wanting to do something based on 'id' from the url, 
here's an example :
  if ($id == 33) {
      print 'You are #33, woohoo!';
A couple interesting/related reads on control structures, such as an if 
statement, and external variables, such as those via the GET method.  
Also note that HTTP_GET_VARS is a predefined variable :
Also, be sure to know and understand these settings :