Study the order

Connect to database => Select database => Set character set => Pass SQL command => Analyze processing result (add, delete, modify) => Processing result set (search)

Database connection and shutdown

  • mysql_connect()
header("Content-type: text/html; charset=utf-8"); // Set the character set
$conn = mysql_connect('localhost'.'username'.'password'); // Return connection identifier on success, false on failure
if($conn){
    echo 'Connection successful';
} else {
    echo 'Connection failed';
}
Copy the code
  • mysql_close()
mysql_close($conn); // Closes the connection for $conn
Copy the code

Open the selected database

  • mysql_select_db()
mysql_select_db('db_name'); // Return true and false
Copy the code

Operating database

  • mysql_query()
mysql_query('set names utf8'); Mysql_query () is used to set the character set
$sql = "Insert into table_name value(' table_name ')"; // The SQL statement must be a string
$query = mysql_query($sql); $SQL can be used for any SQL statement. If $SQL is an INSERT statement, return true or false
if($query){
    echo 'Inserted successfully';
} else {
    echo mysql_error(); // MySQL executes an error message function for SQL statements
}
Copy the code

4FETCH function (function to fetch row data)

  • mysql_fetch_row()
$query = mysql_query('select * from table_name'); // Return the resource identifier on success, false on failure
mysql_fetch_row($query); The data structure is a one-dimensional index array. The index corresponds to the order of the queried fields in the table
Copy the code

When mysql_fetch_ROW () is executed once, the pointer moves down to the second data, and so on. When mysql_fetch_ROW () is executed again to the last data, the pointer points to empty data. Mysql_fetch_row () returns false

  • mysql_fetch_array()

It is used the same way as mysql_fetch_row(), except that mysql_fetch_row() returns an indexed array, while mysql_fetch_array() returns a mixture of indexed and associative arrays

Mysql_fetch_array () has a second argument: 1.mysql_assoc => associative array 2.mysql_num => indexed array 3.mysql_both => both by default

  • mysql_fetch_assoc()

The effect is the same as mysql_fetch_array(‘ resource identifier ‘, MYSQL_ASSOC)

  • mysql_fetch_object()

Same as above, return object, can directly -> access the properties of the object

undefined

  • mysql_num_rows()
$query = mysql_query; $row = mysql_num_row($query);// $row is the number of rows in the result set
Copy the code
  • mysql_result()
id name num price
1 apple 3 13
2 banana 5 45
3 orange 9 89
4 peach 1 32

The database data is shown in the above table

$query = mysql_query("select * from table_name");
echo mysql_result($query, 0, name); // Apples should be output here
// $query specifies the d identifier
// 0 The second argument represents the index of the number of rows, i.e. the (n-1) row
// name Optional parameter Indicates the field name or offset to be displayed. If field name meaning is selected, enter 'name'. If offset meaning is selected, enter 1.
Copy the code
  • mysql_affected_rows()
$conn = mysql_connnect('localhost'.'username'.'password');
mysql_affected_rows($conn);
// The parameter is the connection identifier, which can only get the last operation modification record output value is the number of lines
Copy the code