PHP basic learning-database operations

Source: Internet
Author: User
Tags php database
: This article mainly introduces the database operations of PHP basic learning. if you are interested in the PHP Tutorial, please refer to it. PHP connection database:

Use the mysql_connect () function to connect to the database. the function has three parameters: url, username, and password. the usage is as follows:

$conn = mysql_connect("localhost", "root", "root");

The $ conn variable represents a database connection object, which can be used for subsequent operations, such as creating a database, creating a table, and inserting data.

PHP new database:

$sql = "CREATE DATABASE $db_name";if(mysql_query($sql, $conn)){mysql_select_db($db_name);echo "create db ok, use db $db_name
";}else{echo mysql_error();}
The above code is a PHP script for creating a database. the mysql_query () function is used to execute SQL statements in PHP. this function has two parameters: the first is an SQL statement, the second is the database connection object, which is the $ conn obtained above.

PHP new table:

$sql = "create table test (id int primary key auto_increment,name varchar(20))";if(mysql_query($sql, $conn)){echo "create table ok
";}else{echo mysql_error();}

The code above uses the mysql_query () function to execute the SQL statement for creating a database. the mysql_error () function returns an error message. Note that the last field cannot be followed by a comma when creating a table, otherwise, an error occurs when creating the table. after the table is created, we need to insert data.

PHP inserts data into the database:

$sql = "insert into test(name) values('$name')";mysql_query($sql, $conn);
It's easy to insert data. just use the mysql_query () function to execute the SQL statement.

PHP database SELECT statement:

$cursor = mysql_query("select * from test order by name");while($row = mysql_fetch_array($cursor)){$id = $row["id"];$name = $row["name"];}
The above code uses the mysql_fetch_array () function to traverse records from the database select result set and uses $ row ["id"] to retrieve the record id, use $ row ["name"] to retrieve the name value of the record

The following uses an example to record PHP database operations. In this example, there are two PHP files: index. php and result. php, where index. php is used to input a name value, result. php inserts the name value entered by the user into the database, queries all records from the database, and then displays it on the page. if the database "db_test" has been created ", the table "test" and index are created under the database. the php code is as follows:

 

Then, we receive the input value in result. php, insert it into the database, and display all records in the database. the result. php code is as follows:

 ";echo "IDNAME";while($row = mysql_fetch_array($cursor)){$id = $row["id"];$name = $row["name"];echo "$id$name";}echo "";}else{echo mysql_error();}mysql_close($conn);}?>
Enter the name value and click insert. the following page is displayed:



The above introduces the PHP basic learning database operations, including the content, hope to be helpful to friends interested in PHP tutorials.

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.