Wu Yuxiong - natural born MySQL study notes: MySQL inserting data

MySQL table using the INSERT INTO SQL statements to insert the data.
By MySQL > command prompt window insert data into the data table, or to insert the data via PHP script.
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
The following uses the SQL INSERT INTO statement to insert data into MySQL data tables runoob_tbl
The following examples we will insert three data runoob_tbl table:
root@host# mysql -u root -p password;
Enter password:*******
mysql> use RUNOOB;
Database changed
mysql> INSERT INTO runoob_tbl
    -> (runoob_title, runoob_author, submission_date)
    -> VALUES
    -> ("JAVA 教程", "RUNOOB.COM", '2016-05-06');
Query OK, 1 rows affected (0.00 sec)
mysql>
Italy: 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, does not provide data runoob_id 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.
You can view the data table data by the following statement:
select * from runoob_tbl;
Use PHP script to insert data
PHP can be used in the mysqli_query () function to execute the SQL INSERT INTO command to insert the data.
This function takes two arguments and returns TRUE if executed successfully, otherwise it returns FALSE.
grammar
mysqli_query(connection,query,resultmode);

 

 

Three fields following Examples program data receiving user input, and inserting data in the table:
 < PHP?
dbHost $ = ' localhost: 3306 ' ; // MySQL server host address
dbuser $ = ' root ' ; // MySQL user name
dbpass $ = ' 123456 ' ; // MySQL user name and password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  Die ( ' connection failed: ' mysqli_error ($ Conn).);
}
echo ' successful connection /> <br ' ;
 // set the encoding, to prevent distortion Chinese
mysqli_query($conn , "set names utf8");
 
runoob_title $ = ' learning Python ' ;
$runoob_author = 'RUNOOB.COM';
$submission_date = '2016-03-06';
 
$sql = "INSERT INTO runoob_tbl ".
        "(runoob_title,runoob_author, submission_date) ".
        "VALUES ".
        "('$runoob_title','$runoob_author','$submission_date')";
 
 
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  Die ( ' Unable to insert data: ' mysqli_error ($ Conn).);
}
echo " data insertion success \ n- " ;
mysqli_close($conn);
? > 
To insert data containing the Chinese, you need to add mysqli_query ($ conn, " the SET names utf8 " ); statement.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12113163.html