The Andrew N. Wiggins Consultancy

Contact me at webmaster@anw.biz

Experimental Site

 

This site designed by Byg Software Ltd

 


 
1&1 WEBHOSTING
 

 

 

 
Frequently Asked Questions Overview Search Search
Overview :: 7. Advanced functions: PHP, mySQL, perl, ssh... :: PHP ::Article #3


 

How can I access a mySQL database using PHP?


 

 

The following are four example scripts that deal with Creating a table, Entering data into a table, Viewing a table, Deleting a table.

Script 1: Creating a table:
 

 

        <?php
          /* Start of PHP3 Script */ 
          /* Data of SQL-server */ 
          $server= "db.oneandone.co.uk";     /* Address of 1&1 database server */ 
          $user= "xxxxxx";                   /* FTP-username */ 
          $password= "yyyyyyy";              /* FTP-Password */ 
          $database= "dbxxxxxx";             /* name of database */ 
          $table= "puretest";                /* Name of table, you can select that */ 

          /* Accessing the server and creating the table */ 
          MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
          MYSQL_SELECT_DB($database) or die ( "<H3>database not existent</H3>");
          $result=MYSQL_QUERY( "CREATE TABLE puretest(name varchar(25),email varchar(25))");

          /* Terminate SQL connection*/ 
          MYSQL_CLOSE();
         ?>


 



Script 2: Entering data into a table:
 

         <?php
          /* Start of PHP3 Script */ 
          /* Data of SQL-server */ 
          $server= "db.oneandone.co.uk";     /* Address of 1&1 database server */ 
          $user= "xxxxxx";                   /* FTP-username */ 
          $password= "yyyyyyy";              /* FTP-Password */ 
          $database= "dbxxxxxx";             /* name of database */ 
          $table= "puretest";                /* Name of table, you can select that */ 

          /* Accessing SQL-server */ 
          MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
          MYSQL_SELECT_DB($database) or die ( "<H3>Database non existent</H3>");

          /* Entering the values */ 
          MYSQL_QUERY( "INSERT INTO $table VALUES('OneandOne','info@oneandone.co.uk')");
          MYSQL_QUERY( "INSERT INTO $table VALUES('1and1','info@1and1.com')");
          
          /* Display number of entries */ 
          $anzahl=MYSQL_NUMROWS(MYSQL_QUERY( "SELECT * FROM $table"));
          if ($number==0):
              echo  "database empty";
          elseif ($number > 0):
              echo  "$number rows in database";
          endif;
          /* Close SQL-Connection */ 
          MYSQL_CLOSE();
         ?>



 


Script 3: Display data from a database:
 

         <?php
          /* Start of PHP3 Script */ 
          /* Data of SQL-server */ 
          $server= "db.oneandone.co.uk";     /* Address of 1&1 database server */ 
          $user= "xxxxxx";                   /* FTP-username */ 
          $password= "yyyyyyy";              /* FTP-Password */ 
          $database= "dbxxxxxx";             /* name of database */ 
          $table= "puretest";                /* Name of table, you can select that */ 

          /* Accessing SQL-Server and querying table */ 
          MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
          MYSQL_SELECT_DB($database) or die ( "<H3>Database non existent</H3>");
          $result=MYSQL_QUERY( "SELECT * FROM $table order by name");

          /* Output data into a HTMl table */ 
          echo  "<table  border=\"1\" align=center width=50%"; 
          echo  "<tr>"; 
          echo  "<div color=\"#ffff00\">";
          while ($field=mysql_fetch_field($result))  { 
                 echo  "<th>$field->name</A></th>";
          }
          echo  "</font></tr>"; 
          while($row  =  mysql_fetch_row($result))  { 
                  echo    "<tr>"; 
                  for($i=0;  $i < mysql_num_fields($result);  $i++)  { 
                       echo    "<td align=center>$row[$i]</td>"; 
                  }            
                  echo    "</tr>\n";
          }
          echo  "</table><BR><BR>";   

          /* Close SQL-connection */ 
          MYSQL_CLOSE();
         ?>


 


Script 4: Deleting a table:
 

                            


         <?php
          /* Start of PHP3 Script */ 
          /* Data of SQL-server */ 
          $server= "db.oneandone.co.uk";     /* Address of 1&1 database server */ 
          $user= "xxxxxx";                   /* FTP-username */ 
          $password= "yyyyyyy";              /* FTP-Password */ 
          $database= "dbxxxxxx";             /* name of database */ 
          $table= "puretest";                /* Name of table, you can select that */ 

          /* Accessing SQL-Server and deleting table */ 
          MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
          MYSQL_SELECT_DB($database) or die ( "<H3>Database non existent</H3>");
          $result=MYSQL_QUERY( "DROP TABLE $table");
          echo  "<H1>Table was deleted successfully</H1>";

          /* Close SQL-Connection */ 
          MYSQL_CLOSE();
         ?>


 



Please note that this will only work on the Linux Packages

TuxOnly!

 

Feedback
© 2003 1&1 WEBHOSTING