Php/mysql 3rd-First day (ii)

Source: Internet
Author: User
Tags command line contains insert mysql php code phpinfo unique id mysql database

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:

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

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

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

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 $#@60. The first few lines of PHP. This tag indicates that the PHP code is followed by the $#@62, which 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 back, and now we're going to start with the simplest. If you like, you can also set up PHP, let it use short tags, $#@60;? and $#@62, but this is in conflict with XML, so be careful with it. If you are moving from ASP to PHP, you can even have PHP use $#@60;% and%$#@62 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:

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

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

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

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, 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);

INSERT into Employees VALUES (2,john,roberts,45 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 $#@60; 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.

VI. Testing

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.

  $#@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 ("Name:%s$#@60;br$#@62;\n", mysql_result ($result, 0, "a"));

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

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

printf ("Position:%s$#@60;br$#@62;\n", mysql_result ($result, 0, "Position"));

? $#@62;

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

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

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.

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!



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.