MySQL Query statement

Source: Internet
Author: User

MySQL Query
So far, we've seen a couple of different uses of PHP's mysql_query features, and we'll see more of it in PHP for almost all the work done through MySQL query functionality. We have created a new table and data to insert into the table. In this lesson, we will cover the most common MySQL queries used to retrieve information from the database. The


retrieving data using PHP with MySQL
typically does most of the work with MySQL involves pulling down data from the MySQL database. Data retrieval in MySQL and "select" keywords. Think of the same way you choose to work because it won't be on your computer. If you want to copy some of the data in a file, you will first select the desired information and then copy and paste it.

Choose to use MySQL & from
in trying this lesson, make sure you create a table that contains some data, preferably the same data that we have inserted in MySQL lesson. In this example, we will output the first purpose of MySQL's "instance" table of the Web browser.

<?php
//Make a MySQL Connection
mysql_connect ("localhost", "admin", "1admin") or Die (Mysql_error ());
M ysql_select_db ("test") or Die (Mysql_error ());

//Retrieve All of the data from the "example" table
$result = mysql_query ("SELECT *" 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 MySQL's SELECT statement in PHP. While the MySQL code is very simple, printing out the information with PHP is a bit more involved.

The following is a step-by-step roaming code.

' $ result = mysql_query (SELECT * from for example) '
When you perform a select query of the database, will return a MySQL resource that has everything from your MySQL table, for example. We are going to take advantage of this resource in our PHP code, so we need to store the result in the variable, $.

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

in English, the line's code reads "Each entry selects an example from a table." The asterisk is held in MySQL just told MySQL, including each column of the table.

' $ row = mysql_fetch_array ($ result); '
$ because it is now a MySQL data resource from your MySQL table, for example. The data is stored on the $ results but not yet seen by visitors to your site. When we pass the $ result into the Mysql_fetch_array function-M ysql_fetch_array ($ result)-associative array (name, age) is returned.

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

PHP is case insensitive when you refer to MySQL's column name, so be sure to use uppercase in your PHP code to match the MySQL column name!

To 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'll see how to retrieve each entry into a table and make it into a good format table. However, we recommend that you first understand the PHP and MySQL code before this lesson.

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.