PHP/MySQL three-day connection-first day (2) _ PHP Tutorial

Source: Internet
Author: User
PHPMySQL 3-Day 1 (2 ). 4. if I tell you the first script is over, you will be very happy. The installation process of the software is always unpredictable, because the system and the system are the first script.

If I tell you that you are already sad, you will be very happy. The installation process of the software is always unpredictable, because the system and the system are very different. However, you are lucky to run the database, PHP compilation and installation are complete, and the Web server can correctly process the file with the extension of. php3.

Next we will start to get on the road and write the first script program. Create a text file and add the following content to it:

  $ # @ 60; html $ # @ 62;
$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php
$ Myvar = "Hello World ";
Echo $ myvar;
? $ #@ 62;

$ # @ 60;/body $ # @ 62;
$ # @ 60;/html $ # @ 62;

Now, access the corresponding URL, for example, http: // myserver/test. php3. You can see the text that contains "Hello World" on the page. If you see an error message, check the PHP document 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 of this page, you will find that there is only text like Hello World.

That's because the PHP engine filters out the file content, processes the code, and converts it to standard HTML.

In the above program, the first thing you notice is the delimiters, that is, $ #@ 60 ;? The lines starting with php. This mark indicates that PHP code is followed, and? $ #@ 62; indicates that the code is complete. The strength of PHP is that the code can be put anywhere in a variety of different ways-I mean anywhere. We will see some interesting examples later. now we are still starting from the simplest. If you want to, you can also set PHP to use the short tag, $ # @ 60 ;? And? $ # @ 62;, but this conflicts with XML, so be careful when using it. If you switch from ASP to PHP, you can even use $ # @ 60; % and % $ # @ 62 as the delimiters for PHP.

You will also notice the semicolon behind each line. These semicolons are called separators to separate different commands. You can write all the PHP code in one line and separate the commands with separators. But it looks messy, so we have another line after each semicolon. Remember, each line ends with a semicolon.

Finally, you will notice that the word myvar starts with the $ symbol. This symbol tells PHP that this is a variable. We assign "Hello World" to the variable $ myvar. A variable can be a number or an array. In any case, all variables start with a $ character. <

The real power of PHP comes from its functions. A function is basically a sequence of processing commands. If you compile all the options into PHP, there will be more than 700 functions in total. These functions allow you to do many things.

Now let's add some MySQL content.

V. load databases

Now, we want to add MySQL content. To know which options are included in PHP or some server situations, you can use the phpinfo () function (). Create a program like the following:

  $ # @ 60; html $ # @ 62;
$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php
Phpinfo ();
? $ #@ 62;

$ # @ 60;/body $ # @ 62;
$ # @ 60;/html $ # @ 62;

Save the program and access the file in the browser. You will see some interesting and useful information in the webpage, such. This information is related to the server, the internal environment variables of the Web server, the options contained in PHP, and so on. In the first Extensions, locate the line starting with MySQL. If it is not found, the MySQL support options are not compiled into PHP. You can check the installation procedure and the PHP documentation to see if you have missed anything.

If you find the MySQL line, you can continue.

Before reading data from the MySQL database, we must first put some data into the database. At this stage, there is no easy way to do this. Most PHP programs contain 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 permission table. During installation, a default user (root) is created, which has no password. The database administrator can add users as needed and grant different permissions to users. However, this job can completely write another book, so we only use root users. If you manage servers and databases by yourself, it is important to assign a password to the root user.

In short, let's talk about the database. Sorry for Win32 users, but do some work in DOS. You have to use the DOS window, or type all commands in the "execute" window. Do not forget to enter the directory name of MySQL/bin when entering the command. Unix users can enter commands in the bin directory of MySQL, but the commands must start with./to run the program.

The first thing we need to do is create a database. On the command line, type the following command:

Mysqladmin-u root create mydb

In this way, a database named "mydb" is created. The-u option tells MySQL that we use the root user.

Next, we will add some data. the sample data we use here is a staff database that everyone will like to use. We will use the data files I mentioned earlier. For more information, see the MySQL manual or visit http://www.turbolift.com/mysql/website.

Copy the following text to a file and store the file in the bin directory of MySQL (I assume the file name is mydb. dump ).

  Create table employees (id tinyint (4) DEFAULT 0 NOT NULL
AUTO_INCREMENT, first varchar (20), last varchar (20 ),
Address varchar (255), position varchar (50), 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, Robert TS, 45 There St,
Townville, Telephonist );

Insert into employees VALUES (3, Brad, Johnson, 1/34 Nowhere Blvd,
Snowston, Doorman );

If the text is broken, make sure that each INSERT statement starts with another line. Now, we need to add the data to the mydb database. On the command line, type the following command:

    mysql -u root mydb $#@60; mydb.dump

At this time, you should not encounter any errors. If an error occurs, check whether the error is caused by the text line above.

VI. test

OK. Now we have imported the data into the database. Now let's process the data. Store the following text in a file and store the file in the file directory of the Web server. the suffix is. php3.

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

$ Db = mysql_connect ("localhost", "root ");

Mysql_select_db ("mydb", $ db );

$ Result = mysql_query ("SELECT * FROM employees", $ db );

Printf ("First Name: % s $ # @ 60; br $ # @ 62;", mysql_result ($ result, 0, "first "));

Printf ("Last Name: % s $ # @ 60; br $ # @ 62;", mysql_result ($ result, 0, "last "));

Printf ("Address: % s $ # @ 60; br $ # @ 62;", mysql_result ($ result, 0, "address "));

Printf ("Position: % s $ # @ 60; br $ # @ 62;", mysql_result ($ result, 0, "position "));

? $ #@ 62;

$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

Let me explain the above code. The mysql_connect () function is used to connect to the MySQL database on the specified machine (localhost in this example) with the specified user name (root in this example. If you want to specify a user password, you can also send it to this function. The connection result 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 in a program at the same time, but we are still limited to connecting to one database.

Next, the mysql_query () function completes the most complex part. Using the connection result ID we just obtained, this function sends a row of SQL statements to the MySQL server for processing. The returned result is saved in the variable $ result.

Finally, the mysql_result () function displays the values of each field 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 have never used Perl or C language before, the syntax format of the printf function may look strange. In each of the above programs, % s represents the variable in the second part of the expression (for example, mysql_result ($ result, 0, "position") should be displayed as a string.

We will talk about this lesson. We have successfully compiled, installed, and set up MySQL and PHP, and run a simple program to read information in the database. In the second lesson, we will do more complex work to display the data recorded by multiple rows, and even exchange data with the database.

Continue to work!


Sorry, you will be very happy if I tell you that you are already sad. The installation process of software is always unpredictable, because the process between the system and the system can be said to be thousands...

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.