Mysql query statement

Source: Internet
Author: User
Tags mysql code php and mysql

In php, we often need to execute SQL statements, so we will use mysql_query to query.

MySQL Query
So far, we have seen a couple of different uses of PHP's mysql_query function, we will see that almost all the work in PHP MySQL is through the MySQL query function. We have created a new table and inserted data into it. In this lesson, we will cover the most common MySQL queries, databases used to retrieve information.


Use PHP and MySQL to retrieve data
Most of the work is usually done with MySQL involving pulling data from the MySQL database. Keyword for data retrieval and "selection" in MySQL. Think about choosing the same method for work because it will not be on your computer. If you want to copy a copy of some documents, you will first select the required information, and then copy and paste it.

Select MySQL & from
When trying this lesson, make sure that you have created a table that contains some data, preferably the same data. The lessons we have inserted in MySQL. In this example, we will output the web browser of the first MySQL "instance" table.

<? Php
// Make a MySQL Connection
Mysql_connect ("localhost", "admin", "1 admin") or die (mysql_error ());
Mysql_select_db ("test") or die (mysql_error ());

// Retrieve all the data from the "example" table
$ Result = mysql_query ("SELECT * FROM example ")
Or die (mysql_error ());

// Store the record of the "example" table into $ row
$ Row = mysql_fetch_array ($ result );
// Print out the contents of the entry

Echo "Name:". $ row ['name'];
Echo "Age:". $ row ['age'];

?>

This is an example of how to use the MySQL SELECT statement in PHP. Although the MySQL code is very simple, the printed information is a bit more involved with PHP.

The following is the code for one-step roaming.

'$ Result = mysql_query ("select * slave for example ")'
When you execute the SELECT query, the database will return a MySQL resource that owns everything from your MySQL table ", for example ". We need to use this resource in our PHP code, so we need to store the result in variable $.

Select * from example'
Yes, this is part of the same code line, but we want to explain this MySQL statement in more detail!

In English, the code for this row reads "example of selecting an entry from a table ". The asterisks are stored externally in the Table in each column that MySQL just told MySQL.

'$ ROW = mysql_fetch_array ($ result );'
$ Because it is a MySQL DATA resource from your MySQL table ", for example ". The data is stored in $ results, but has not yet seen visitors on your website. When we use the $ result to include mysql_fetch_array function-m ysql_fetch_array ($ result)-join array (name, age) is returned.

In our MySQL table "for example," there are only two fields, we care about: name and age. The key to these names is to extract data, our associated array. In order to let us use the name $ row ['name'], let our age use $ row ['age'].

PHP is case-sensitive. When you refer to the names of MySQL columns, you must use the names of MySQL columns that match your PHP code in uppercase!

Continue as an Example
In this lesson, we learned how to get the first item from a MySQL table and output to the browser using PHP. In the next lesson, we will see how to extract each entry to a table and convert it into a good format table. However, we recommend that you first understand the lessons learned before PHP and MySQL code.

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.