Php_mysql Tutorial-First day _php tutorial

Source: Internet
Author: User
Tags informix php and mysql phpinfo
First page Php/mysql Introduction
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.

Fourth page 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:
Copy CodeThe code is as follows:


$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 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 following, and now we'll start with the simplest. If you prefer, you can also set up PHP to use a short tag, , but this conflicts with XML, so use it with care. 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.
Fifth page Loading 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:
Copy CodeThe code is as follows:


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.
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, address V Archar (255), Position varchar (), PRIMARY KEY (ID), UNIQUE ID (ID)), INSERT into Employees VALUES (1, ' Bob ', ' Smith ', ' He Re 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.

NET page Sixth test
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.
Copy CodeThe code is as follows:


$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.

http://www.bkjia.com/PHPjc/318121.html www.bkjia.com true http://www.bkjia.com/PHPjc/318121.html techarticle First page Php/mysql introduction you should have heard of open source software (OSS) unless you have been living on Mars for the last 6-8 months. This movement has a huge impact, has caused a ...

  • 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.