Treenut Home | programming-notes | mysql

The following is information on some work I have done with PHP/MySQL programming on system running at 1and1.com.

I discovered fairly quickly that all web sites that purport to provide training in MySQL are not the same. Many provided instruction examples that flat-out don't work (perhaps they are for an older version of MySQL or are for a different implementation. The best site I have found so far is the following: http://www.w3schools.com/php/php_mysql_intro.asp

The code presented runs on the 1and1.com implementation. I also like the programming style they use, in that they separate command syntax from command content. This clearly displays what belongs to mySQL and what belongs to the particular database.

1and1 programming notes:

  1. The MySQL server is located behind a firewall and cannot be accessed directly from 'outside'. You cannot program a web site running on another system to access databases running on 1and1 servers. This is very logical and reassuring in this day and age.
  2. The easiest way to define a database (and monitor your progress in programming to it) is with the utility MySQLAdmin. On 1and1 the only way to do this is by running the MySQLAdmin from your 1 and 1 Control Panel with MySQL Administration option under the 'Web Space' Menu. This, again, is due to the firewall.
  3. 1and1 has many servers (more than one running MySQL) and the server running your web site is not the same as the server running MySQL so you cannot use localhost as your servername in the mysql_connect command.

Some personal adaptations not present in the W3Schools examples:

I like to make use of constants in my programs. This allows me to provide labels to some of the data thereby identifying it according to it's function within the program logic and syntax. This way I don't have to remember the data specific to each server implementation or database definition when I'm writing and maintaining mySQL commands within the program. I have defined the following 'constants' (using ficticious database number 123456789):

$servername="db123456789.db.1and1.com";
$username="dbo123456789";
$password="my-password";
$database="db123456789";

The values that you assign to these variables are displayed in the heading part of the MySQL Administration page.

These are then used to build the following variable:

$con = mysql_connect($servername, $username, $password);

Which is then used in the following programming example which opens the datebase on the 1and1 server:

$con = mysql_connect($servername, $username, $password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database, $con); ... mysql_close($con);

Applying these small modifications, I have followed the programming examples on the W3schools site and implemented the code by simply cutting and pasting into my PHP / HTML code framework to perform each database access task on my site. I simply creage a file containing the housekeeping commands listed above which I then use as a template for all my php files that access this database.