Leilin Peng Share: MySQL inserting data

  MySQL table using the INSERT INTO SQL statements to insert the data.

  You can mysql> command prompt window to insert data into the data table, or to insert data through PHP scripts.

  grammar

  The following is a generic insert data into MySQL data tables INSERT INTO SQL syntax:

  INSERT INTO table_name ( field1, field2,...fieldN )

  VALUES

  (Value1, value2, ... valueN);

  If the data is character, you must use single or double quotes, such as: "value".

  Command prompt window insert data

  Below we will use the SQL INSERT INTO statement to insert data into MySQL data tables codercto_tbl

  Examples

  The following examples we will insert three data codercto_tbl table:

  root@host# mysql -u root -p password;

  Enter password:*******

  mysql> use CODERCTO;

  Database changed

  mysql> INSERT INTO codercto_tbl

  -> (codercto_title, codercto_author, submission_date)

  -> VALUES

  -> ( "Learning PHP", "agricultural tutorial code", NOW ());

  Query OK, 1 rows affected, 1 warnings (0.01 sec)

  mysql> INSERT INTO codercto_tbl

  -> (codercto_title, codercto_author, submission_date)

  -> VALUES

  -> ( "Learning MySQL", "agricultural tutorial code", NOW ());

  Query OK, 1 rows affected, 1 warnings (0.01 sec)

  mysql> INSERT INTO codercto_tbl

  -> (codercto_title, codercto_author, submission_date)

  -> VALUES

  -> ( "JAVA Tutorial", "CODERCTO.COM", '2016-05-06');

  Query OK, 1 rows affected (0.00 sec)

  mysql>

  Note: Use the arrow -> is not part of the SQL statement, it only represents a new line, if a SQL statement is too long, we can create a new line by the Enter key to writing SQL statements, SQL command statement terminator is semicolon;.

  In the above example, we do not provide codercto_id data, because the field we've set it to AUTO_INCREMENT (automatic increase) properties when you create the table. So, the field is automatically incremented without the need for us to set up. Example NOW () is a MySQL function that returns the date and time.

  Next we can view the data in the data table by the following statement:

  Read the data sheet:

  select * from codercto_tbl;

  Use PHP script to insert data

  You can use PHP's mysqli_query () function to execute SQL INSERT INTO command to insert data.

  This function takes two arguments and returns TRUE if executed successfully, otherwise it returns FALSE.

  grammar

  mysqli_query(connection,query,resultmode);

  Parameter Description

  connection required. MySQL provisions connection to use.

  query the necessary provisions query string.

  resultmode optional. A constant. It can be any of the following values:

  MYSQLI_USE_RESULT (If you need to retrieve large amounts of data, use this)

  MYSQLI_STORE_RESULT (default)

  Examples

  The following examples are three data fields for receiving user input program, and insert the data table:

  adding data

   '; // set the encoding, to prevent distortion Chinese mysqli_query ($ conn, "set names utf8"); $ codercto_title =' learning Python '; $ codercto_author =' CODERCTO.COM '; $ submission_date =' 2016-03-06 '; .. $ sql = "INSERT INTO codercto_tbl" "(codercto_title, codercto_author, submission_date)" "VALUES" "( '$ codercto_title', '$ codercto_author', '$ submission_date')";. mysqli_select_db ($ conn, 'CODERCTO' ); $ retval = mysqli_query ($ conn, $ sql); if ($ retval)! {die ( 'Unable to insert data:'. mysqli_error ($ conn));} echo "data insertion success \ n"; mysqli_close ($ conn);?>

  For data containing the Chinese insert, you need to add mysqli_query ($ conn, "set names utf8"); statement.

  Next we can view the data in the data table by the following statement:

  Read the data sheet:

  select * from codercto_tbl;

  Click to see all MySQL Tutorial Articles: https://www.codercto.com/courses/l/30.html (edit: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/pengpeng1208/p/10973499.html