You are Here:
FAQ
Scripting and Programming Languages
PHP
Article #4
How to access MySQL database using PHP scriptThe following are four example scripts that deal with creating a table, entering data into a table, viewing a table and deleting a table. Creating a table
<?php
/* Start of PHP3 Script */
/* Data of SQL-server */
$server= "db1.perfora.net"; /* Address of 1&1 database server */
$user= "xxxxxx"; /* Database username */
$password= "yyyyyyy"; /* Database Password */
$database= "dbxxxxxx"; /* name of database */
$table= "puretest"; /* Name of table */
/* 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();
?>
Entering data into a table
<?php
/* Start of PHP3 Script */
/* Data of SQL-server */
$server= "db1.perfora.net"; /* Address of 1&1 database server */
$user= "xxxxxx"; /* Database user name */
$password= "yyyyyyy"; /* Database 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 */
$number=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();
?>
Display data from a table
<?php
/* Start of PHP3 Script */
/* Data of SQL-server */
$server= "db1.perfora.net"; /* Address of 1&1 database server */
$user= "xxxxxx"; /* Database user name */
$password= "yyyyyyy"; /* Database 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();
?>
Deleting a table
<?php
/* Start of PHP3 Script */
/* Data of SQL-server */
$server= "db1.perfora.net"; /* Address of 1&1 database server */
$user= "xxxxxx"; /* Database user name */
$password= "yyyyyyy"; /* Database 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();
?>
Disclaimer: 1&1 provides the scripts and related information on this page as a courtesy, subject to 1&1's General Terms and Conditions of Service (the "GT&C"). As set forth in more detail in the GT&C, the scripts and information are provided "as-is", without any warranty, and 1&1 is not liable for any damages resulting from your use of the scripts or information. |
© 2008 1&1 Internet Inc - About 1&1 Internet