title: url?id=1 How do i GET the id using a PHP MYSQL function?
body: <p>I am learning how to use functions to organize my code better and cant seem to workout a mysql query that requires an external variable such as the <strong><em>url?id=1</em></strong>. How is a <strong><em>mysql query function to get a variable from the url</em></strong> done?</p>
  
  <p>How do i achieve this?</p>
  
  <p>My function:</p>
  
  <pre><code>function item() { 
  
  $itemid = $_GET['id'];
  
  $t = "items";       
  $sql = mysql_query("SELECT * FROM $t WHERE id=$itemid") or die(mysql_error());  
  $r = mysql_fetch_array( $sql );
  
  $id         = $r['id'];
  $date       = $r['date'];
  $title      = $r['title'];
  $location   = $r['location'];
  
  }
  </code></pre>
  
  <p>Now when i call that function inside another file like this:</p>
  
  <pre><code>&lt;?php ### // no entry
  
  item(); // get item function
  
  ?&gt;
  
  &lt;h1&gt;Item View&lt;/h1&gt;
  
  &lt;?php echo $id;?&gt;
  &lt;?php echo $date;?&gt;
  &lt;?php echo $title;?&gt;
  &lt;?php echo $location;?&gt;
  </code></pre>
  
  <p>I get no result from the above, i did test the function to make sure it was being called by printing some html and it does get called. It only works if i move the query inside the view file.</p>
  
  <p>I have a strange feeling im missing something important that is to do with a php class as well, but i have been racking my brain online trying to understand examples.</p>
  
  <p>If someone could please elaborate with a very simple example of how this is achieved i would be very grateful :-)</p>
  
  <p>Update:</p>
  
  <pre><code>function external_db_connect() {
  
  $database_hostname = 'localhost';
  $database_username = 'user';
  $database_password = 'pass';
  $database_name     = 'table';
  class DatabaseConnectClass {
  public $database_hostname;
  public $database_username;
  public $database_password;
  public $database_name;
  public function databaseConnection($objDatabaseConnect) {
  $this-&gt;_database_connection =
  mysql_pconnect($this-&gt;database_hostname,
  $this-&gt;database_username,
  $this-&gt;database_password)
  or trigger_error(mysql_error(),E_USER_ERROR);
  return $this-&gt;_database_connection;
  }
  public function databaseConnectionSelect() {
  $this-&gt;_database_connection_select =
  mysql_select_db($this-&gt;database_name,
  $this-&gt;_database_connection);
  return $this-&gt;_database_connection_select;
  }
  public function databaseConnectionProcess($objDatabaseConnect) {
  $objDatabaseConnect-&gt;databaseConnection($objDatabaseConnect);
  $objDatabaseConnect-&gt;databaseConnectionSelect($objDatabaseConnect);
  }
  public function databaseConnectionMain($objDatabaseConnect) {
  $objDatabaseConnect-&gt;databaseConnectionProcess($objDatabaseConnect);
  }}
  
  $objDatabaseConnect = new DatabaseConnectClass();           
  $objDatabaseConnect-&gt;database_hostname = $database_hostname;
  $objDatabaseConnect-&gt;database_username = $database_username;
  $objDatabaseConnect-&gt;database_password = $database_password;
  $objDatabaseConnect-&gt;database_name     = $database_name;
  $objDatabaseConnect-&gt;databaseConnectionMain($objDatabaseConnect);   
  
  }
  </code></pre>
  
  <p>Thanks in advance.
  Jonny</p>