PHP & MYSQL

PHP has a fast and easy2use extenstion to MYSQL.
We will try to demonstrate some of the ways of working with PHP & MYSQL.

Creating a common connection

Creating a connection to the MYSQL database
A common connection is mostly used, since you then dont need to specify witch connection mysql commands shold use.

 
<?php
 
  $server   = "localhost";
  $username = "john";
  $password = "secretpassword";
 
  /* creating connection */ 
  mysql_connect($server,$username,$password); 
 
?>

Creating a specify connection

Creating a specify connection, is mostly used in cases where you access two or more databases at the same time

 
<?php
 
  $localhost_server   = "localhost";
  $localhost_username = "john";
  $localhost_password = "secretpassword";
 
  /* creating connection to localhost */ 
  $localhost_connection = mysql_connect($localhost_server,$localhost_username,$localhost_password); 
 
  $greenquery_server   = "greenquery";
  $greenquery_username = "john";
  $greenquery_password = "secretpassword";
 
  /* creating connection to greenquery */ 
  $greenquery_connection = mysql_connect($greenquery_server,$greenquery_username,$greenquery_password); 
 
?>

Working with the connection

With a connection to the MYSQL server. Then we can list the current databases with SQL.

 
<?php
 
 
  $server   = "localhost";
  $username = "john";
  $password = "secretpassword";
 
  /* creating connection */ 
  mysql_connect($server,$username,$password); 
 
  /* execute a SQL to show all databases and store them in $result */
  $result = mysql_query("show databases");
 
  /* A resultset from MYSQL
   * is like a table. One row per record   
   * and then a column for every field.
   */
  while ($row = mysql_fetch_array($result)){
     // read the firtst column in every row
     $database[] = $row[0]; // 0 mean first column, 1 would be next column.
  }
 
  //close connection to mysql
  mysql_close();
 
  // Print alle databases on screen
 
  for ($i=0;$i<count($database);$i++){
     // print database name
     print "Database : " . $database[$i] . "<br />\n";
  }
 
?>

Working with a database

Working on a specify database.

 
<?php
 
 
  $server   = "localhost";
  $username = "john";
  $password = "secretpassword";
  $database = "test";
 
  /* creating connection */ 
  mysql_connect($server,$username,$password); 
 
  /* tell mysql witch database to use */
  mysql_select_db($database);
 
?>

Working with a table

When working with a MYSQL table, then it will accessed with SQL.

A Test table

  CREATE TABLE MyTest(
     id int NOT NULL AUTO_INCREMENT,
     myName text,
     myEmail text,
     PRIMARY KEY(id)
  )

Accessing MyTest table in MYSQL

<?php
 
 
  $server   = "localhost";
  $username = "john";
  $password = "secretpassword";
  $database = "test";
 
  /* creating connection */ 
  mysql_connect($server,$username,$password); 
 
  /* tell mysql witch database to use */
  mysql_select_db($database);
 
  /* creating SQL to fetch john from MyTest */
  $sql = "select * from MyTest where myName='John'";
 
  /* executing SQL to fetch John */
  $result = mysql_query($sql);
 
  /* read the result */
 
  while ($row=mysql_fetch_array($result)){
    print "I found " . $row['myName'] . "<br />";
  }  
 
  /* close connection to mysql */
  mysql_close();
 
?>
 
php_mysql.txt · Last modified: 2007/09/09 19:30 by michael
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki