PHP basic operations for the database

Source: Internet
Author: User

If you are using the phpstudy you recommend, you can do the following, if the other PHP environment may vary.

Into the background of the database, in the browser address bar input localhost/phpmyadmin can enter the database, the default user name and user password are root;

Here you can build a database, build a table, insert data into a table, it's simple, just like word, you don't need a command statement.

But if you want to manipulate the data table in PHP, you still need the MySQL statement.

The following commands are the code that operates on the database in PHP.

Link database:

$lin = mysql_connect (' localhost ', ' root ', ' root ');

The mysql_connect () function opens the MySQL connection.

LocalHost, this is your local server, root: username, root: password. ( corresponding order )

Working with databases:

mysql_select_db (' Feest ', $lin);

The mysql_select_db () function sets the active MySQL database.

Feest: Database name, $lin: MySQL connection.

Query the data in one of the tables in the database:

$check = mysql_query ("select * from yonghu where username= ' 123");

Yonghu: Is the name of the table you want to query, username: is the field you want to query.

The meaning of this sentence is: Query Yonghu table inside whether there is a field username is 123 this data.

* This symbol represents all the data in the query Yonghu, * This position represents the meaning of the keyword .

As an example :

  $check = mysql_query ("Select username from Yonghu);

Query the data in the Yonghu table field named username.

This time you want to echo $check This variable is echo does not come out, will echo out Resource ID # This, need to use Mysql_fatch_array the query results to traverse and take out the data into an array or a variable.

 $guanjian = Mysql_fetch_array ($check);

This will return you an array so that you can get the data in the array, for example: Echo $guanjian [' username '];

It will echo out the value of the field username in the table.

Query the database to meet the number of query criteria:

mysql_num_rows ()

As an example:

$ini = mysql_query ("select * from Yonghu where username = ' 123 '");

$hue = mysql_num_rows ($ini);


At this point, Echo $hue, the number of rows in your table that match the criteria, is the number that meets the requirements. ( login can use this to write.) )

PHP basic operations for the database

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.