This paper introduces how to build a network database application method by MySQL of the golden combination of Web database, PHP is a server-side embedded hypertext Processing language similar to Microsoft ASP, it is a powerful tool to build dynamic website.
While MySQL is a lightweight SQL database server that runs on a variety of platforms, including Windows NT and Linux, and has a GPL version, MySQL is considered the best product for building a database-driven dynamic Web site. PHP, MySQL, and Apache are the best partners for Linux platform sites. This tutorial is a 3rd course that is described in detail from installation to application.
Introduction to MySQL
You should have heard of open source software (OSS) unless you have lived on Mars for the last 6-8 months. The movement has a tremendous impact and has attracted the attention of some big companies. Like Oralce, Informix, and a number of companies are starting to migrate their main database products to one of OSS's products-Linux operating systems.
If you have enough technical power, having a complex and huge relational database system (RDBMS) is a powerful force. But maybe you're just getting started with the database, and you've just read Jay's article and decided to work on a data-driven Web site. But you may find that you lack the necessary resources to run an ASP server or an expensive database system (and you don't need those things). You need some free, UNIX-enabled stuff.
Then I suggest you use PHP and MySQL. Together, these two things are the best combination for developing data-driven Web sites. In fact, I don't have to bother to explain. The number of hosts applying PHP jumped from 7,500 units in June 1998 to 410,000 in March 1999, according to an unofficial survey organized by Netcraft. Nice, huh? The combination of the two software also won the annual Database Product award at the Webcon98 conference, and won a beautiful trophy.
MySQL is a small and long database server software, for small (and not necessarily very small) application system is very ideal. In addition to supporting standard ANSI SQL statements, it supports a wide variety of platforms, while on UNIX systems the software supports multithreading and can achieve fairly good performance. For users who do not use UNIX, it can be run as a system service on a Windows NT system or run as a normal process on a Windows 95/98 system.
PHP is a scripting language for server-side interpretation. If you have contacted ASP, you should be more familiar with embedding code in HTML pages. The PHP code is interpreted at the end of the server into a normal HTML page content and sent to one end of the browser. This pattern allows us to use it to perform quite complex functions.
In addition to the free one (of course, MySQL also has some restrictions on licensing), the Php-mysql combination can also run across platforms, which means you can develop on Windows and then run Qiaoqi on UNIX platforms duo Yili HP can run as a standard CGI process, At this point it is a standalone script interpreter, or an embedded module of Apache
If you are interested in using a different database server, PHP also supports Informix, Oracle, Sybase, solid and PostgreSQL, and common ODBC.
PHP supports some cutting-edge technologies for Internet development. These technologies include identity authentication, XML, dynamic image generation, WDDX, shared memory, and dynamic PDF documents, among other things. If you are not satisfied, PHP is very easy to expand, so as long as you have the ability to programming, you can do your best.
Finally, the two kinds of software are developed by a large number of programmers, so there are many support methods such as document and mailing list. Bug fixes are fast, and if you ask for a new feature, someone will always consider your request and implement it if the feasibility is high enough.
That's enough! Let's take a look at what's in this tutorial.
The first lesson is about installing both software in a UNIX and Windows environment. If you are not too concerned about this (perhaps you are developing it on your ISP's server), you can skip to the first sample program and start your wonderful journey there.
In the second lesson, we're going to learn some more complex scripting features like looping, processing user input, and exchanging data with databases, and so on.
The third lesson is about validation and how to make your scripts clear and concise.
Here we go
Second, the installation of MySQL
Move quickly, get these packages, and study them carefully. It's not an easy thing to do. You have a number of options for getting packages, compiling packages, and installing packages. Let's start with MySQL because we have it to run PHP.
The MySQL Central website is http://www.mysql.com/. For the convenience of people downloading (the software is still relatively large), there are many mirror sites around the world. For more efficient use of the Internet, please find a nearby website to download.
At this time you will also have a variety of options. If you are a fan, you can download the source code, otherwise, there are a variety of platforms on the Internet to run the precompiled binaries can be downloaded directly.
In addition, MySQL has a shared software version for Windows users, with a slightly lower version number. If you want the latest version, you have to pay for a software license. MySQL also provides ODBC drivers that enable front-end applications to access the MySQL database. Some other details are available on its website and you can go and see it.
Pre-compiled versions of UNIX and Windows are simple enough to be used without much explanation. So, let's introduce the source code compilation. Windows users remember that you need to run the MYSQLD program, which is in the Mysql/bin directory.
Download the compressed package file and put it in a directory. Use gzip and tar to decompress and unpack. The quicker approach is to use the following commands:
Gunzip $#@60; mysql-xxxx.tar.gz | Tar xvf-
Where xxxx is a version of your own tag. The above command will create a directory named Mysql-xxxx, all source program files are in this directory. Go to the directory by performing the CD mysql-xxxx command, and carefully read the multiple Readme file install files. These files can be a big help when it comes to some problems.
MySQL comes with some handy configuration scripting programs. Simply type./configure, and you can let these programs do a lot of work for you. If you need to explicitly specify what you want to do, you can use the./configure-help command, which lists the options you can use. For example, if you are compiling on a machine with less memory, you can use the--with-low-memory option. I like to install MySQL in a general directory, rather than installing it in multiple directories on the machine, so I specify the installation directory and specify the-prefix option.
You can also specify many other options, such as what to skip in the compilation, what to keep, and so on. Let's assume that we're all installed in the server's/usr/local/mysql directory. This means that we will type the./configure-prefix=/usr/local/mysql command.
When the configuration script runs, it checks your system, and then generates some files to ensure that the compilation goes smoothly. If the script fails, it also displays some useful error messages that tell you the cause of the failure. You often encounter scripting programs that fail to find multithreaded library files. At this point you should check to see if Mit-pthreads is installed in the system, and if not, install the software. Linux users must install Linuxthreads. These library files are critical to MySQL's multithreaded running mode, which is running multiple versions of itself.
If all goes well, simply type the make command, and then you can go for a cup of coffee. MySQL is a fairly complex program that can be compiled for a while. If you find an error, you can check the document to see if you have missed some of the preparations that are relevant to your operating system.
Next, type the make install command, and all required files will be installed in the appropriate directory. You are almost ready to start using! If you are new to MySQL and have never installed MySQL before, you have to create a default permission to install it, so type ... scripts/mysql_install_db to make the appropriate settings.
That's it. Our preparations have been completed. The next thing to do is to start the database automatically when the machine starts and shut down the database automatically when the machine is powered down. Yes, this work also has a special scripting program. The Mysql.server Start command can start the database, and Mysql.server stop can shut down the database. It's a simple one. If you want to start the database manually (so you don't have to reboot the machine), you go to the top-level directory where MySQL is installed (/usr/local/mysql) and type the BIN/SAFE_MYSQLD & command.
We're halfway through. Next is the PHP section.
Third, the installation of PHP
Well, when I see this, I hope you've installed MySQL and run it. That was so much fun! Here is the PHP ... This process is easier, but the large number of options are dazzling. Of course, you don't have to panic. You can always start over, recompile PHP, and Add or remove an option as needed.
PHP's home in http://www.php.net/. This site has a huge amount of information, from development project content to software error reporting. As with MySQL, you can choose a mirror to download from your most recent site. Obviously, you get the downloads part of the website to download PHP.
You don't have a lot of choices here. There are some pre-selected binaries, but that's all experimental. If you are not on the Windows platform, download the source code and compile it yourself.
But let's talk about Windows first. When using PHP, it is common practice to develop on a Windows system and then run on a UNIX server. You may end up choosing this approach, which requires you to be familiar with the installation under both platforms.
After you download the Windows binaries, you can extract the files from the package into the PHP3 directory in the C disk with any ZIP decompression program you like. The Readme file in the package gives a partial description of the details of the installation process. But we're here to make a reader's Digest of the key content in this file: If you don't want to install PHP in the C:PHP3 directory, you'll have to edit the. inf file in the uncompressed file.
In the PhP3 directory, you will find many. dll files. Move all of the. dll files that do not start with Php_ to the Windowssystem directory. Then, change the name of the Php.ini-dist file, change it to Php3.ini, and move it to the Windows directory. If you open this file, you will find that there are a lot of interesting things to change. Now, put the containing:
Extension=php3_mysql.dll
The comments on the line of content are removed.
If you are using the Apache server on the Win32 platform, set up Apache so that it can identify and interpret PHP files. You need to add the next line to the http.conf file or srm.conf file (depending on which file you are using the version of the Apache software):
Action application/x-httpd-php3 "Php3/php.exe"
AddType application/x-httpd-php3 PhP3
Or, if you are using IIS, right-click the Php_iis_reg.inf file and choose Install. You will have to reboot the system to take effect on the changes that you just made.
OK, Finish windows, and say UNIX. Of course, we're going to start compiling from the source code. Similar to MySQL, download and unpack the source files. PHP also contains a configuration script, but you cannot use the default settings entirely. Run./configure-help | More commands, you can see what new, interesting options are available on a per page basis. You must choose whether to compile PHP into an external CGI program or an Apache inline module. If you are using the Apache Web server and you can recompile it, choose the inline module, which is faster and easier to use. Otherwise, you can choose the CGI method. In addition, you have to specify that the support section of MySQL be compiled
We now assume that you want to compile within the nested module and have the MySQL Support section. If you subsequently need to add additional options or library files, you can add them later. Type the following command:
./configure-with-apache=/path/to/apache/dir-with-mysql=/usr/local/mysql
If you plan to compile with an external CGI program, remove the-with-apache option. When the configuration program is run, the appropriate system files are created. You can then simply execute the make command.
You can have another cup of coffee. Don't worry if you feel restless at this time. Everyone has a bit of a close feeling when they first install PHP. Just drink some more coffee.
If you choose a CGI program to compile, you can now use it. Just copy the resulting executable file to the CGI program directory. If you are compiling using the Apache inline module, perform the make install command and copy the files to the Apache directory. In this directory, you can follow the Apache documentation instructions, add PHP modules to it, and recompile Apache.
Now you have to tell the Web server how to process the page content through a PHP program. If you're not using Apache, you'll need to look up the documentation for the Web server software to see how to get it to handle a file with a suffix of. PhP3. The apache1.3.x version of the user should only be included in the httpd.conf or srm.conf file: AddType application/x-httpd-php3.php3. If you're using a CGI program, you'll have to add the following in front of AddType:
Action application/x-httpd-php3 "Php3/php.exe
That's it. Your luck is not so bad, now MySQL is also running, PHP is also working properly.
Now that we have done so much, we can practice the drill below.
Iv. First Script
If I tell you that the real sadness has passed, you will be very happy. The software installation process is always unpredictable, because the system can be said to be different from the system. But you're lucky, the database runs, PHP compiles and installs, and the Web server can handle files with the extension. php3 correctly.
We're on our way down to the first scripting program. Create a text file in which to add the following:
$myvar = "Hello World";
Echo $myvar;
?>
Now, access the appropriate URL, for example, Http://myserver/test.php3. You should be able to see the words "Hello World" on the page. If you see an error message, check the PHP documentation to see if the software settings are correct.
That's it! This is your first PHP program. If you look at the HTML source code for this page, you'll find only words like Hello world.
That's because the PHP engine filters the contents of the file, processes the code, and translates it into standard HTML.
The first thing you notice in the above program is the delimiter, which means the end of the code. The power of PHP is that the code can be placed anywhere in many different ways-I mean anywhere. We'll see some interesting examples back then, and now we're going to start with the simplest. If you want, you can also set up PHP to use short tags, but this will conflict with XML, so be careful to use it. If you are moving from ASP to PHP, you can even use PHP as a delimiter.
You will also notice the semicolon following each line. These points are called delimiters and are used to separate different instructions. You can write all the PHP code in one line and separate the commands with the delimiter. But that looks messy, so we have a different line after each semicolon. Remember, the end of each line ends with a semicolon.
Finally, you will notice that the word MyVar begins with the $ symbol. This symbol tells PHP that this is a variable. We assign "Hello world" to variable $myvar. A variable can be a number or an array. In any case, all variables begin with a $ character.
The real power of PHP comes from its functions. function, which is essentially a sequence of processing instructions. If you compile all the options into PHP, there will be more than 700 functions in total. These functions allow you to do a lot of things.
Now let's add some MySQL content. >>
V. Loading the database
Now we're going to join the MySQL content. An easy way to find out what options are included in PHP or some of the server aspects is to use the function phpinfo (). Create a program like the following:
Phpinfo ();
?>
Save this program to access this file in the browser. You'll see that the page contains some interesting, useful information, like this. This information is about the server, the internal environment variables for the Web server, the options contained in PHP, and so on. In the first paragraph extensions, locate the line that starts with MySQL. If not, it means that the MySQL support option is not compiled into PHP. You can check the installation steps again and check the PHP documentation to see if you missed anything.
If you find the MySQL line, you can continue.
Before reading data from a MySQL database, we have to put some data in the database. There is no easy way to do this at this stage. Most PHP programs come with a data file that contains data to create and activate the MySQL database. This process is not within the scope of this tutorial, so let me do it for you.
MySQL uses its own table of user rights. When installed, a default user (root) is created, and the user does not have a password. The database administrator can add users and give users different permissions as needed, but this work can be done with a separate book, so we only use the root user. If you manage servers and databases yourself, it is important to assign a password to the root user.
Anyway, let's go on to the database. For WIN32 users, I'm sorry, but you have to do some work under DOS. You have to use a DOS window, or type all the commands in the Execute window. Don't forget to bring the Mysql/bin directory name when you enter the command. UNIX users can enter a command in the MySQL bin directory, but the command must start with a./To allow the program to run.
The first thing we have to do is actually create a database. At the command line, type the following command: mysqladmin-u Root Create MyDB This creates a database named "MyDB". The-u option tells MySQL that we are using the root user.
Next, we want to add some data, here we use the sample data is everyone like to use the employee database. We'll use the data files I mentioned earlier. If you want to learn more about this, you can check out the manuals that are available with MySQL or visit the http://www.turbolift.com/mysql/Web site.
Copy the following text into a file and place the file in the MySQL Bin directory (I assume the filename is mydb.dump).
CREATE TABLE Employees (ID tinyint (4) DEFAULT ' 0 ' not NULL
auto_increment, varchar, last varchar (20),
Address varchar (255), Position varchar, PRIMARY KEY (ID),
UNIQUE ID (ID); INSERT into Employees VALUES (1, ' Bob ', ' Smith ',
' 128 here St, CityName ', ' Marketing Manager ');
INSERT into Employees VALUES (2, ' John ', ' Roberts ', ' There St,
Townville ', ' telephonist ');
INSERT into Employees VALUES (3, ' Brad ', ' Johnson ', ' 1/34 nowhere Blvd,
Snowston ', ' doorman ');
If the text is a broken line, make sure that each INSERT statement is a different line. Now we're going to add the data to the MyDB database. At the command line, type the following command:
Mysql-u Root MyDB < Mydb.dump
You should not encounter any errors at this point. If you do make a mistake, check carefully to see if the text in the line above causes an error. >>
VI. Testing
OK, now we've imported the data into the database. Now let's deal with the data. Put the following text into a file, the file exists in the Web server's document directory, suffix named. php3.
$db = mysql_connect ("localhost", "root");
mysql_select_db ("MyDB", $db);
$result = mysql_query ("SELECT * FROM Employees", $DB);
printf ("Name:%s
", Mysql_result ($result, 0," a "));
printf ("Last Name:%s
", Mysql_result ($result, 0," last "));
printf ("Address:%s
", Mysql_result ($result, 0," address "));
printf ("Position:%s
", Mysql_result ($result, 0," position "));
?>
Let me explain the code above. The mysql_connect () function is responsible for connecting to the MySQL database on the specified machine (in this case the machine is native localhost) with the specified user name (the username in this case is root). If you want to specify a user password, you can also give it to this function. The result of the connection is saved in the variable wishdb.
Then the mysql_select_db () function tells PHP that the database we want to read is mydb. We can connect to multiple databases on multiple machines at the same time in the program, but we are still limited to connecting a database at the moment.
Next, the mysql_query () function completes the most complex part. Using the result of the connection you just obtained, the function sends a row of SQL statements to the MySQL server for processing. The returned results are saved in the variable $result.
Finally, the mysql_result () function displays the values of the fields that are obtained by the SQL query command. With the variable $result, we can find the first record, the record number is 0, and the values of each field are displayed.
If you haven't used Perl or C before, then the syntax of the printf function will look strange. In each of the above lines,%s represents the variable in the second part of the expression (for example, mysql_result ($result, 0, "position") should be displayed as a string. To learn more about printf, see your PHP documentation.
If the text is a broken line, make sure that each INSERT statement is a different line. Now we're going to add the data to the MyDB database. At the command line, type the following command:
Mysql-u Root MyDB < Mydb.dump
You should not encounter any errors at this point. If you do make a mistake, check carefully to see if the text in the line above causes an error. >>
This is the lesson we'll talk about here. We have successfully compiled, installed, and set up MySQL and PHP and run a simple program to read the information in the database.