PHP basic methods of manipulating MySQL

Source: Internet
Author: User
Tags php mysql

Basic knowledge of PHP Operational database


1: Set the header to UTF-8 encoding to prevent garbled.

Header ("Content-type:text/html;charset=utf-8");



2: There are several steps to connecting a database

1: First step, connect to MySQL server

$conn =mysql_connect ("localhost", "root", "12345"); If the [email protected] symbol is in front, the error message is ignored.


You can also use Die () to display the connection failure information.

@mysql_connect ("localhost", "root", "12345") or Die (' Database connection failed '. Mysql_error ());


2: The second step, when we use the mysql_connect () function to connect to the database successfully, we need to select the db of the database

mysql_select_db (database name);

mysql_select_db (school) or Die (' This database not found '. Mysql_error ());

mysql_query ("Set names UTF8"); Set the character set of the database


3: The third step, select the table from the connected database, and get the table data, that is, prepare the statement

$query = "SELECT * Form Class";

$result =mysql_query ($query);


4: Fourth step, display the data in the recordset

Mysql_fetch_array ($result);

5: The last step, close the database

Mysql_close ($conn)



3:php MySQL additions and deletions to the search

Prepare database statements: $query =insert into Class (Name,age,sex) VALUES (' Cary ', ' 29 ', ' Male ');

Execute database mysql_query ($query);


Change: $query = "Update class set age=40 where Name= ' Cary '";

mysql_query ($query);


Delete: $query = "Delete from class where name= ' Cary '";

mysql_query ($query);


Query: $query = "Select Name,age,sex from Class";

$result =mysql_query ($query);


while ($row =mysql_fetch_array ($result)) {

echo $row [' name ']


}


In this way, we store the queried statements in the variable $result result set.



Some of the functions commonly used in 4:php

Mysql_fetch_array ()----Get data


This article is from the "Cary_qin blog" blog, make sure to keep this source http://xpqinqun.blog.51cto.com/2136/1605353

PHP basic methods of manipulating MySQL

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.