Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
MySQL First, Php/mysql introduction
You should have heard of open source software (OSS) unless you've been living 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 the products of OSS-the Linux operating system.
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 free this (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 on UNIX platforms. In addition, PHP can be used as a standard CGI process to run, at this time it is a stand-alone script interpreter, or Apache is an embedded module.
If you are interested in using a different database server, PHP also supports Informix, Oracle, Sybase, solid, and PostgreSQL, as well as 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 own.
The last thing to say is that both software are developed by a large number of programmers, so there are many ways to support documents and mailing lists. 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 want to learn some more complex scripting features, such as 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 it 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 it's the only way 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 MySQL 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 < 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 your system, and if not, install the software. Linux users must install the linuxthreads. These library files are critical for MySQL's multithreaded running mode, which is to run 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 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're almost ready to start using it! 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 just too easy. 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've finished half of it. 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 again, 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 are here to make a reader's Digest of the key content in this document: If you do not want to install PHP in the C:\PHP3 directory, but other directories, then you have to edit the extracted files in the. inf 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 \Windows\System 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 can be changed. Now, put the containing:
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):
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 make the changes you have just made effective.
OK, Finish windows, and then 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 portion of MySQL will be compiled.
We now assume that you want to compile within the nested module and have the MySQL Support section. If you later need to add additional options or library files, you can add them later. Type the following command:
If you intend to compile with an external CGI program, please remove the-with-apache option. When the configuration program is run, the appropriate system files are created. The next step is to simply execute the make command.
You can have a cup of coffee again. If you feel restless at this time, don't worry. 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, then you can use it now. 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. Users of the Apache 1.3.x version are only required to include 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. Don't forget to check the FAQ and software documentation When you encounter a problem. You can also try an e-mail discussion group.
Now that we have done so much, we can practice the drill below. >>
Four, the first script
If I tell you that the really sad one 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";
Now, access the appropriate URL, for example, Http://myserver/test.php3. You should be able to see the words "Hello World" in 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 is the line starting with <?php. This tag is followed by the PHP code, and?> represents 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 in the back, and now we're going to start with the simplest. If you want, you can also set up PHP so that it uses short tags, "and", but this is in conflict with XML, so be careful with it. If you are moving from ASP to PHP, you can even make PHP use <% and%> as delimiters.
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 it can be 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 in some MySQL content. >>
V. Loading of databases
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 such as the following:
Save this program and access the 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. At this stage, there is no easy way to do this thing. 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 back 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 need 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 that I mentioned earlier. If you want to learn more about this, you can check out the manual or access that you have with MySQL Http://www.turbolift.com/mysql website.
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 ');
Inseonist '); RT into Employees VALUES (2, ' John ', ' Roberts ', ' There St,
Townville ', ' Teleph
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 on 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. >>
OK, now we've imported the data into the database. Now let's take care of 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);
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 $db.
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 each field that the SQL query command obtains. 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.
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. In the second lesson, we will do more complex work to display data from multiple rows of records and even exchange data with the database.
Keep working on it!
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
and provide relevant evidence. A staff member will contact you within 5 working days.