Php_mysql Tutorial-First day _php Foundation

Source: Internet
Author: User
Tags documentation informix php and mysql phpinfo unique id
First page 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 best.
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.

Page Fourth 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:
Copy Code code as follows:

<body>
<?php
$myvar = "Hello World";
Echo $myvar;
?>
</body>

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.
Fifth page Load 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 such as the following:
Copy Code code as follows:

<body>
<?php
Phpinfo ();
?>
</body>

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 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 (a), last varchar, addre SS varchar (255), Position varchar (x), 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 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.

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

<body>
<?php
$db = mysql_connect ("localhost", "root");
mysql_select_db ("MyDB", $db);
$result = mysql_query ("SELECT * FROM Employees", $DB);
printf ("Name:%s<br>n", mysql_result ($result, 0, "a"));
printf ("Last Name:%s<br>n", mysql_result ($result, 0, "last"));
printf ("Address:%s<br>n", mysql_result ($result, 0, "address"));
printf ("Position:%s<br>n", mysql_result ($result, 0, "Position"));
?>
</body>

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.

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.