Home  >  Article  >  Backend Development  >  PHP_MySQL Tutorial-Day 1_PHP Tutorial

PHP_MySQL Tutorial-Day 1_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:07799browse

Page 1 Introduction to PHP/MySQL
You should have heard of Open Source Software (OSS) unless you have been living on Mars for the last six to eight months. This movement has huge impact and has attracted the attention of some major companies. Companies like Oralce, Informix, and many others have begun to port their main database products to one of OSS's products - the Linux operating system.
If you have enough technical strength, having a complex and huge relational database system (RDBMS) will be even more powerful. But maybe you are just getting started with databases. You just read Jay's article and decided to build a data-driven website yourself. However, you may find that you lack the necessary resources to run an ASP server or an expensive database system (and you don't need these things). You need something free, Unix-ready.
Then I recommend you to use PHP and MySQL. Together, these two things are the perfect combination for developing data-driven websites. In fact, I don’t need to waste any time explaining it. An unofficial survey organized by Netcraft showed that the number of hosts using PHP jumped from 7,500 in June 1998 to 410,000 in March 1999. Not bad, right? The combination of these two software also won the Database Product of the Year Award at the Webcon98 conference and received a beautiful trophy.
MySQL is a small and exquisite database server software, which is very ideal for small (of course not necessarily small) application systems. In addition to supporting standard ANSI SQL statements, it also supports multiple platforms. On Unix systems, the software supports multi-threaded operation, which can achieve very good performance. For users who do not use Unix, it can run as a system service on Windows NT systems, or as a normal process on Windows 95/98 systems.
PHP is a server-side interpreted scripting language. If you have been exposed to ASP, then you should be familiar with embedding code in HTML pages. The PHP code is interpreted and converted into ordinary HTML page content on the server side and sent to the browser side. This mode allows us to use it to complete quite complex functions.
In addition to being free (of course, MySQL also has some licensing restrictions), the PHP-MySQL combination also runs cross-platform, which means you can develop on Windows and then run on a Unix platform. In addition, PHP can also run as a standard CGI process, in which case it is a stand-alone script interpreter or an embedded module of Apache.
If you are interested in using other database servers, PHP also supports Informix, Oracle, Sybase, Solid and PostgreSQL, as well as common ODBC.
PHP supports some cutting-edge technologies in Internet development. These technologies include authentication, XML, dynamic image generation, WDDX, shared memory, and dynamic PDF documents, to name a few. If you're still not satisfied, PHP is very easy to extend, so as long as you have programming skills, you can show off your skills yourself.
The last thing to say is that both softwares are developed by a large number of programmers, so there are many support methods such as documentation and mailing lists. Bugs are fixed quickly, and if you ask for a new feature, someone will consider it and implement it if it's feasible.
Enough said! Let’s take a look at what’s included in this tutorial.
The first lesson is about installing these two software in Unix and Windows environments. If you don't care much about this issue (maybe you are developing on the ISP's server), you can jump directly to the first sample program and start your wonderful journey from there.
In the second lesson, we will learn some more complex scripting functions, such as looping, processing user input, exchanging data with the database, etc.
The third lesson is about the confirmation function and how to make your script clear and concise.
Let’s get started.

Page 4 First Script
You will be happy if I tell you that the really sad part is over. The software installation process is always unpredictable, because it can be said to be vastly different from system to system. But you are lucky. The database is running, PHP has been compiled and installed, and the web server can correctly handle files with the extension .php3.
Let’s start officially and write the first script program. Create a text file and add the following content to it:

Copy the code The code is as follows:



$myvar = "Hello World";
echo $myvar;
?>



Now, visit the corresponding URL, for example, http://myserver/test.php3. You should see the text containing "Hello World" on the page. 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 of this page, you will find that there is only text like Hello World in it.
That’s because the PHP engine filters the file content, processes the code in it, and converts it into standard HTML.
The first thing you may notice in the above program is the delimiters, which are the lines starting with indicates the end of the code. The power of PHP is that this code can be placed anywhere - and I mean anywhere - in many different ways. We'll see some interesting examples later, but for now let's start with the simplest one. If you prefer, you can set up PHP to use the short tags, , but this will conflict with XML, so use it with caution. If you're moving from ASP to PHP, you can even have PHP use <% and %> as delimiters.
You will also notice the semicolon after each line. These semicolons are called delimiters and are used to separate different instructions. You can write all your PHP code on one line, using delimiters to separate commands. But that looks messy, so we put a new line after each semicolon. Remember, each line must end with a semicolon.
Finally, you will notice that the word myvar starts with a $ 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 an array. Regardless, all variables begin with the $ sign.
The real power of PHP comes from its functions. Functions, basically process sequences of instructions. If you compiled all options into PHP, there would be over 700 functions in total. These functions let you do a lot of things.
Now let’s add some MySQL content.
Page 5: Loading the database
Now, we have to add the MySQL content. An easy way to find out what options are included in PHP, or what's happening on the server side, is to use the function phpinfo(). Create a program like the following:
Copy the code The code is as follows:


phpinfo();
?>



Save this program and access the file in your browser. You'll see that the page contains some interesting and useful information, like this one. This information is about the server, web server internal environment variables, options included in PHP, etc. In the first section of Extensions, find the line starting with MySQL. If it is not found, it means that the MySQL support option is not compiled into PHP. You can double-check the installation steps and consult the PHP documentation to see if you missed anything.
If the MySQL line is found, 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 come with a data file that contains some data to create and activate the MySQL database. This process is outside the scope of this tutorial, so let me do it for you.
MySQL uses its own user permissions table. During installation, a default user (root) will be created, which does not have a password. The database administrator can add users and grant users various permissions as needed, but this work can be written in another book, so we only use the root user. If you manage the server and database yourself, it is important to assign a password to the root user.
Anyway, let’s continue talking about the database. Sorry for Win32 users, but you have to do some work under DOS. You have to use a DOS window, or type all commands in the Execute window. Don’t forget to include 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 ./ in order for the program to run.
The first thing we need to do is actually create the 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 step, we need to add some data. The example data we use here is the employee database that everyone likes to use. We'll be using the data files I mentioned earlier. If you want to know more about this, you can check the manual provided by MySQL or visit the http://www.turbolift.com/mysql/ website.
Copy the following text to a file and save 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', 'Roberts','45 There St , Townville','Telephonist');
INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman');
If the text is wrapped, please make sure that each INSERT statement starts on a new line. Now, we are 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 to see if the error is caused by the wrapping of the text above.

net Page Six Test
OK, now we have imported the data into the database. Now let's process this data. Save the following text into a file, and save the file in the document directory of the Web server with the suffix .php3.
Copy code The 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?>



Let me explain the above code. The mysql_connect() function is responsible for connecting to the MySQL database on the specified machine (in this case the machine is 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 pass 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 currently we are limited to connecting to one database.
Next, the mysql_query() function completes the most complex part. Using the connection result identifier just obtained, this function sends a row of SQL statements to the MySQL server for processing. The returned result is stored in the variable $result.
Finally, the mysql_result() function displays the values ​​of each field obtained by the SQL query command. Using the variable $result, we can find the first record, the record number is 0, and display the values ​​of each field in it.
If you have not used Perl or C language before, the syntax format of the printf function will seem strange. In each line of the program above, %s represents which variable in the second part of the expression (for example, mysql_result($result,0,"position")) should be displayed as a string. For a more in-depth understanding of printf, see the PHP documentation.
That’s it for this lesson. We have successfully compiled, installed and set up MySQL and PHP, and run a simple program to read information from the database. In the second lesson, we will do some more complex work to display data from multiple rows and even exchange data with the database.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318121.htmlTechArticleFirst page PHP/MySQL introduction You should have heard of Open Source Software (OSS), unless you have arrived in the past six years You've been living on Mars for eight months. This movement has a huge impact and has caused a...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn