MySQL study notes (on)

SQL injection principle during the analysis of the time, weak grasp the MySQL database, reference rookie tutorial MySQL MySQL Tutorial speed Brush

About MySQL

MySQL is the most popular relational database management systems, in terms of WEB MySQL is one of the best RDBMS applications. (RDBMS Relational Database Mangagement System: relational database management software)

What is a database, keywords, etc. ......

...... (copy and paste refused meaningless, Portal )

PHP script to operate the database usage

MySQL connection

Using the mysqli_connect()function to connect to the database;
mysqli_connect(host, username, password, dbname, port, socket)

parameter description
host Optional. Host name or ip address
username Optional. MySQL user name
password Optional. MySQL password
dbname Optional. Use of database
port Optional. Try the port number to connect to MySQL server
socket Optional. Provision socket or named pipe to use

Use mysqli_close()function can be disconnected from the link MySQL database, but generally do not need, because non-persistent open connections are automatically closed after the script is finished.

<?php
$dbhost = 'localhost';  // mysql服务器主机地址
$dbuser = 'root';            // mysql用户名
$dbpass = '123456';          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Could not connect: ' . mysqli_error());
}
echo '数据库连接成功!';
mysqli_close($conn);
?>

Create a database

CREATE DATABASE 数据库名;

Use PHP script mysqli_query()function to create or delete a database;
mysqli_query(connection, query, resultmode);

parameter description
connection essential. MySQL provisions connection to use
query essential. String to query
resultmode Optional. A constant, which can be: MYSQLI_USE_RESULT (If you need to retrieve a large amount of data, use this) MYSQLI_STORE_RESULT (default)

Create a database instance:

<?php
    $dbhost = 'localhost';
    $dbuname = 'root';
    $dbpasswd = 'root';
    $conn = mysqli_connect($dbhost,$dbuname,$dbpasswd);
    if(!$conn)
    {
        die('Could not connect :'.mysqli_error());
    }
    echo 'Successfully!!';
    
    $sql = 'CREATE DATABASE RUNOOB';
    $retval = mysqli_query($conn,$sql);
    if(! $retval)
    {
        die('Failing to create databse!').mysqli_error($conn);
    }
    echo "<br>Successfully to create the database<br>";

    mysqli_close($conn);

?>

Found in the name of the database is part of the case, all lowercase, uppercase statement is to create, find time to see the name of the database are lowercase.

Delete Database

In the MySQL command line, use the drop command
drop database <数据库名>

Similar in PHP script, mainly through the previously explained mysqli_query()to execute a SQL statement function.

So we can ......

<?php
    $dbhost = 'localhost';
    $dbuname = 'root';
    $dbpasswd = 'root';
    $conn = mysqli_connect($dbhost,$dbuname,$dbpasswd);
    if(!$conn)
    {
        die('Could not connect :'.mysqli_error());
    }
    echo 'Successfully!!';
    
    $sql = 'DROP DATABASE RUNOOB';
    $retval = mysqli_query($conn,$sql);
    if(! $retval)
    {
        die('Failing to DROP databse!').mysqli_error($conn);
    }
    echo "<br>Successfully to DROP the database<br>";

    mysqli_close($conn);

?>
Select Database

When we have a lot of time to manage the database, faced with complex business, switch back and forth database is essential.

At the command line, a command use 数据库名称will be able to switch databases, this time I saw the word ...... ......

Note: All database names, table names, table fields are case sensitive. So you need to enter the correct name when using SQL commands.

Oh? ? Really so embarrassing? I decided to try it yourself, first of all I use CREATE DATABASE RUNOOBsuccessfully created a database, then, I use the statement CREATE DATABASE RuNOOB, the result creation fails, which indirectly proves my earlier database names are case insensitive statement, but bold assertion that the error code ? After the fact, I looked up some information, the following conclusions:

  • MySQL under Linux, database names, table names, column names, aliases capitalization rule is this:
    • Database names, table names, aliases, variable names are case-sensitive list of strict
    • Column name, column alias in all cases is to ignore case
  • MySQL on Windows are not case sensitive

Of course, not static, in Linux, eliminate case-sensitive by modifying the configuration file /etc/my.cnf, add lower_case_table_names = 1; Windows, you can in the MySQL configuration file my.inito add. Case-insensitive parameter represents 0, 1 represents a case-insensitive parameter. Reference article 1
Reference Articles 2

So ...... where were we? Oh, select the database, continue;

PHP script using the function mysqli_select_db()to select a database. After the successful execution of the function returns TRUE, otherwise return FALSE;

mysqli_select_db(connection, dbname);

parameter description
connection essential. MySQL provisions connection to use
dbname essential. It provides for the use of database
Create a data table

Information needed to create data tables are:

  • Table Name
  • Table Field Name
  • The definition of each table field

grammar:CREATE TABLE table_name (column_name column_type);

Create a data table with the PHP script:
still mysqli_query()function executed successfully returns TRUE, failed to return FALSE
, remember? It is the three parameters of Kazakhstan, forgetting the words to turn up the pages.

The second mandatory parameters which queryrepresentatives are part of our main function, SQL statements, comprehensive introduction to the SQL language can open another one up.

Delete Data Sheet

Delete operation is very dangerous, delete the database, the repository will be lost, too, removed the data table, the data table will be lost.

grammar:DROP TABLE table_name ;

In PHP script, mysqli_query()function

supplement:

  • Delete data within tables, use the Delete :
    DELETE FROM 表名 WHERE 删除条件;
  • Clear the data tables, the retention structure table with TRUNCATE :
    TRUNCATE TABLE 表名;
Insert data

grammar:

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"

Copy from the environment I came out as a test of the success of the code:

<?php
    $dbhost = 'localhost';
    $dbuname = 'root';
    $dbpasswd = 'root';
    $conn = mysqli_connect($dbhost,$dbuname,$dbpasswd);
    if(!$conn)
    { 
        die('Could not connect :'.mysqli_error());
    }
    echo 'Successfully!!';
    /*Above is about if successfully connecting to the database*/
    mysqli_query($conn,"set names utf8");
    
    $runoob_name = 'blackcat';
    $runoob_id = 1;
    $runoob_time = '2019-6-1';
    
    $sql = "INSERT INTO runoob_tb1 (id,name,dates) VALUES ".
            "('$runoob_id','$runoob_name','$runoob_time')";
    
    mysqli_select_db($conn,'RUNOOB');
    
    $retval = mysqli_query($conn,$sql);
    if(! $retval)
    {
        die('<br>Failing to do').mysqli_error($conn);
        
    }
    echo "<br>Successfully to do<br>";

    mysqli_close($conn);

?>

Of course, this is a prerequisite, in the INSERT statement, parentheses before VALUES is the field name, that is, the name of each column, after VALUES as we insert the data, which is the number of them and other types are corresponding, but here buried pit: three variable names behind the VALUES clause, why use single quotes?

Query data

grammar:

SELECT column_name, column_name
FROM table_name
[WHERE Clause]
[ LIMIT N ] [ OFFSET M ]
  • Queries may use one or more tables, using a comma ( ,) separated;
  • SELECT command reads one or more records
  • You can use the wildcard asterisk ( *) instead of the other fields,
  • WHERE used to set the query may contain any conditions
  • LIMIT is set using the number of records returned
  • OFFSET offset data using the SELECT statement to specify the start of the query. Offset 0 is default,

After the success of query data, using the PHP function mysqli_fetch_array()with the second parameter MYSQLI_ASSOC set the parameter query result is returned associative array, another function mysqli_fetch_assoc()of the function row as an associative array from the result set. It returns an associative array generated from the acquired result set, if no multi-line, returns false.

Line simply is the use of this function can query results are returned as an array index for the field name, when the function mysqli_fetch_array()of the second parameter is MYSQLI_NUM , it returns an array of numbers, the use of digital content index obtaining records.

Guess you like

Origin www.cnblogs.com/x-anonymous/p/MySQLStudyNotes_1.html