PHP and MySQL 3rd pass-first day _php

Source: Internet
Author: User
Keywords first yes file if PHP we MySQL database
Tags informix php and mysql phpinfo
I. Introduction of PHP/MYSQL

You should have heard of open source software (OSS) unless you have been living on Mars for the last 6-8 months. The movement has a huge impact and has attracted the attention of some big companies. Like Oralce, Informix, and a number of companies are starting to transplant their main database products to one of the products of OSS-Linux operating system.

If you have enough technical power, having a complex and large relational database system (RDBMS) is a powerful one. But maybe you're just getting started with the database, and you just read Jay's article and decide you're going to have a data-driven website. However, you may find that you lack the necessary resources to run an ASP server or an expensive database system (you do not need these 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 the job of developing a data-driven website. In fact, I don't need to explain more. A non-official survey by Netcraft showed that the number of hosts that applied PHP jumped from 7,500 in June 1998 to 410,000 units in March 1999. Isn't that good? The combination of the two software also won the annual Database Product award at the Webcon98 Conference, and a beautiful trophy.

MySQL is a small database server software, ideal for small (and certainly not very small) applications. In addition to supporting standard ANSI SQL statements, it supports a variety of platforms, and on UNIX systems the software supports multi-threaded operation, which allows for fairly good performance. For users who do not use UNIX, it can be run as a system service on a Windows NT system or as a normal process on a Windows 95/98 system.

PHP is a scripting language for server-side interpretation. If you have been exposed to ASP, then you should be familiar with embedding code in HTML pages. The PHP code is interpreted as a normal HTML page content at one end of the server and is given to the browser at one end. This pattern allows us to use it to accomplish quite complex functions.

In addition to free this (of course, MySQL also has some restrictions on licensing), Php-mysql's portfolio can also be run across platforms, which means you can develop on Windows and run on UNIX platforms. In addition, PHP can be used as a standard CGI process, when it is a standalone script interpreter, or an Apache embedded module.

If you are interested in using other database servers, PHP also supports Informix, Oracle, Sybase, solid and PostgreSQL, and General ODBC.

PHP supports some of the leading edge technologies for Internet development. These technologies include identity authentication, XML, dynamic image generation, WDDX, shared memory, and dynamic PDF documents. If you are not satisfied, PHP is easy to expand, so as long as you have the programming ability, you can do your best.
Finally, both of these software are developed collaboratively by a large number of programmers, so there are many ways to support documents and mailing lists. Bug fixes are quick, and if you ask for new features, someone will always consider your request and implement it if it is feasible enough.

That's enough! Let's take a look at some of the things in this tutorial.

The first lesson is about installing both software in UNIX and Windows environments. If you're not too concerned about the problem (perhaps you developed it on your ISP's server), you can jump straight to the first sample program and start your magical journey from there.

In the second lesson, we want to learn some more complex scripting features, such as looping, handling user input, exchanging data with a database, and so on.

The third lesson is to confirm the function and how to make your script clear and concise.

Here we go. >>
Second, install MySQL

Hurry up and get these packages and study them carefully. It's not an easy thing to do. You have a lot 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 site is http://www.mysql.com/. In order to facilitate people to download the use (this 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 an enthusiast, you can download the source code; otherwise, there are pre-compiled binaries running on various platforms on the web that can be downloaded directly.

In addition, MySQL has a shared software version for Windows users, which has 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 of the other details are available on its website and you can take a look.

The pre-compiled versions of UNIX and Windows are simple enough to be unpacked and used without much explanation. So let's take a look at the source code compilation. For Windows users, remember that you need to run the MYSQLD program, which is under the Mysql/bin directory.

Download the compressed package file and place it in a directory. Use gzip and tar to decompress and unpack the package. The quick way to do this is to use the following command:

Gunzip < mysql-xxxx.tar.gz | Tar xvf-

Where xxxx is a version tag that you take. The above command will create a directory named Mysql-xxxx, where all the source program files are in this directory. Go to the directory by executing the CD MYSQL-XXXX command, and read through the multiple Readme file install files carefully. These files can be a big help when you encounter some problems.

MySQL comes with some handy configuration scripts. Simply type in the./configure, and you can have 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 total directory instead of being installed in multiple directories on the machine, so I'll specify the installation directory and specify the-prefix option.

You can also specify a number of other options, such as what to skip during compilation, which parts to keep, and so on. We might as well assume that you want to install it all 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 is running smoothly. If the script fails, it also displays some useful error messages that tell you why the failure occurred. You will often encounter a script that fails while looking for a multithreaded library file. At this point you should check to see if Mit-pthreads is installed on the system, and if not, install the software. Linux users must install the linuxthreads. These library files are critical for MySQL's multithreaded run mode (that is, running multiple versions of its own).

If everything goes well, simply type the make command and then you can go for a cup of coffee. MySQL is a fairly complex program that compiles for a period of time. If you find an error, you can check the documentation to see if you have missed some of the preparations related to your operating system.

Next, type the make install command, and all the required files will be installed in the appropriate directory. You're almost ready to start using it! If you're new to MySQL and you've never installed MySQL before, you'll need to create a default license to install it, so type ... scripts/mysql_install_db to set it up accordingly.

That's it. Our preparations are complete. The next thing to do is to start the database automatically when the machine starts and shut down the database automatically when the computer shuts down. Yes, there is also a dedicated scripting program for this job. The Mysql.server Start command can start the database, and Mysql.server stop can shut down the database. It's really easy. If you want to start the database manually (so you don't have to restart the machine), you go to the top-level directory where you installed MySQL (/usr/local/mysql) and type Bin/safe_mysqld & commands.

We have finished half of it. Next is the PHP section. >>
Third, install PHP

Well, when you see this, I hope you've installed and run MySQL. That's really fun! Here is the PHP ... The process is easier, but the vast array of options is confusing. Of course, you don't have to panic. You can always start over and recompile PHP, adding or removing an option as needed.

PHP's home in http://www.php.net/. The site is very informative, from development project content to software error reporting. As with MySQL, you can choose a mirror site that is closest to you to download. 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-compiled 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, the general practice is to develop on Windows systems and then run on UNIX servers. You may end up choosing this option, which requires you to be familiar with the installation of both platforms.

After you download the Windows binaries, you can unzip the files in the package to the PHP3 directory in the C drive using any of your favorite zip decompressor. The Readme file in the package explains some of the details of the installation process, but here's a reader's Digest of the key content in this file: If you don't want to install PHP in the C:\PHP3 directory, instead of the other directory, you have to edit the. inf file in the extracted file.

In the PhP3 directory, you will find many. dll files. Move all the. dll files that do not start with the Php_ file name to the \Windows\System directory. Then, change the php.ini-dist file name, 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 comment for that line of content is removed.

If you are using an Apache server on the Win32 platform, set up Apache so that it can recognize and interpret PHP files. You need to add the following line to the http.conf file or to the srm.conf file (which is depending on the version of the Apache software you are using):

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 select Install. You will have to restart your system to make the changes you just made effective.

OK, finish talking about Windows, and then say UNIX. Of course, we're going to start compiling from the source code. Similar to MySQL, the source files are downloaded and unpacked. PHP also contains a configuration script, but you cannot use the default settings entirely. Run./configure-help | The more command, you can see what new and 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 an Apache Web server and you can recompile it, choose the inline module mode, which is faster and easier to use. Otherwise, you can choose the CGI mode. In addition, you have to specify that the support section of MySQL will be compiled.

We now assume that we want to compile it inline and with the MySQL Support section. If you later 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 intend to compile with an external CGI program, remove the-with-apache option. When the configuration program runs, the appropriate system files are created. The next step is simply to execute the make command.

You can have a cup of coffee again. If you're feeling restless at the moment, don't worry. Everyone has a bit of a close-up feeling when they first install PHP. Just drink a little more coffee.

If you choose the CGI program method to compile, you can now use it. Just copy the resulting executable file to the CGI program directory. If you compile using the Apache inline module, you will need to execute the make install command to copy the files to the Apache directory. In this directory, you can follow Apache's documentation, 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 are not using Apache, then you need to consult the documentation for the Web server software to see how to get it to handle the file suffix. php3. Users of the Apache 1.3.x version must only include the following 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 running, PHP is working properly. Don't forget to check out the FAQs and software documentation when you encounter problems. You can also try e-mail discussion groups.

Now that we've done so much, we can actually drill down here. >>

Four, the first script

If I tell you that the real sadness is over, you will be very happy. Software installation process is always unpredictable, because the system and the system can be said to be very diverse. But you're lucky, the database is running, PHP is compiled and installed, and the Web server can handle files with the. php3 extension correctly.

We're on our way down the road, and we're going to write the first script. Create a text file in which to add the following content:




$myvar = \ "Hello world\";
Echo $myvar;
?>

 

Now, access the appropriate URL, for example, Http://myserver/test.php3. You should see the text in the page that contains "Hello world". If you see an error message, check the PHP documentation to see if the software is set up correctly.

That's it! This is your first PHP program. If you look at the HTML source code for this page, you will find only text like Hello World in it.

That's because the PHP engine filters the contents of the file, processing the code in it, and converting it into standard HTML.

The first thing you notice in the program above may be the delimiter, which is the? lt; The few lines that PHP started with. This token indicates that the PHP code is followed, and?> indicates 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 following, and now we'll start with the simplest. If you prefer, you can also set PHP to use a short tag, but this conflicts with the XML, so use it carefully. If you're moving from ASP to PHP, you can even have 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 a delimiter. But that looks messy, so we'll start with a separate line behind each semicolon. Remember, each line ends with a semicolon.

Finally, you will notice that the word MyVar begins with the $ sign. This symbol tells PHP that this is a variable. We assign "Hello World" to the variable $myvar. A variable can be a number, or it can be an array. However, all variables start with the $ symbol.

The real power of PHP comes from its functions. function, which is basically 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 in. >>
V. Loading the database

Now, we're going to join the MySQL content. An easy way to know what options are included in PHP, or in some cases on the server, is to use the function phpinfo (). Create a program such as the following:




Phpinfo ();
?>

 

Save this program and access this file in your browser. You'll see that the Web page contains some interesting and useful information, like this. This information is about the server, the Web server internal environment variables, the options included in PHP, and so on. In the first paragraph of extensions, find a line starting with MySQL. If not found, then the MySQL support option is not compiled into PHP. You can check the installation steps again and check out the PHP documentation to see if you missed anything.

If you find the MySQL line, you can continue.

Before we read the data from the MySQL database, we have to put some data into the database first. At this stage, there is no easy way to do this. Most PHP programs come with a data file that contains some 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 user rights table. At the time of installation, 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 another 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 ahead and say 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 enter the command with the Mysql/bin directory name. UNIX users can enter commands in the MySQL bin directory, but the command must start with./To get the program running.

The first thing we want to do is actually create the database. Under 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're going to add some data, and the sample data we're using here is the employee database that everyone likes to use. We will use the data files that I mentioned earlier. If you want to know more about this, you can check out the manuals in MySQL or visit the http://www.turbolift.com/mysql/website. <
Br>
Copy the following text into a file that exists in the MySQL bin directory (I assume the file name is Mydb.dump).

CREATE TABLE Employees (ID tinyint (4) DEFAULT \ ' 0\ ' not NULL
Auto_increment, first varchar, last varchar (20),
Address varchar (255), Position varchar (a), PRIMARY KEY (ID),
UNIQUE ID (ID)); INSERT into Employees VALUES (1,\ ' bob\ ', \ ' smith\ ',
\ ' 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 wrapped, 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 time. If something goes wrong, please check carefully if the above text is broken to cause an error. >>
VI. Testing

OK, now that we have imported the data into the database. Now let's take care of this data. Put the following text into a file, the file exists in the Web server's document directory, the suffix name is. php3.




$db = mysql_connect (\ "localhost\", \ "root\");
mysql_select_db (\ "Mydb\", $db);
$result = mysql_query (\ "SELECT * from Employees\", $db);
printf (\ "First Name:%s
\n\ ", Mysql_result ($result, 0,\" first\ "));
printf (\ "Last Name:%s
\n\ ", Mysql_result ($result, 0,\" last\ "));
printf (\ "Address:%s
\n\ ", Mysql_result ($result, 0,\" address\ "));
printf (\ "Position:%s
\n\ ", 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 (in this case, the user name is root). If you want to specify a user password, you can also send it to this function. The result of the connection is saved in the variable $db.

The mysql_select_db () function then 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 our program, but we are still limited to a single database at the moment.

Next, the mysql_query () function completes the most complex part. Using the connection result ID 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 individual fields that are obtained by the SQL query command. Using 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 in the past, the syntax format of the printf function can be very strange. In each of the above lines of the program,%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 the PHP documentation.

This is the lesson we're going to talk about here. We have successfully compiled, installed, and set up MySQL and PHP, and have run a simple program to read the information in the database. In the second lesson, we will do some more complicated work to show the data of multiple rows of records and even exchange data with the database.

Keep working on it!

  • Related Article

    Contact Us

    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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.