Create a database and credit by PHP MySQL source disk to build the basics

First, the environment to build
XAMPP environment I use to build credit disc source q <319.135.5031> php and mysql environment is very simple.
Problems: XAMPP phpMyAdmin not in use, can not be directly used phpMyAdmin to create the tables.
Native environment: win7 x64 + IE8 two, manually create tables in the MySql
Description: XAMPP mysql database created using a root is a blank password account, after using such a manner, the operation can be directly
connected to the database 1.mysql :
shell> MySQL
- is not a privilege of the way into the account.
shell> MySQL -u root
- After the way the root account permissions
2. Data change password:
shell> the SET PASSWORD the FOR 'root' @ 'localhost' = PASSWORD ( 'password')
in this way to change the password to log in database, would require a password, use the following command line
the shell> MySQL -u -p the root
password :( carriage return after, where a password)
3. Create a database:
the shell> the CREATE tABLE tbl_name;
4. Create a table:
below is a table created from simple to complex the whole process.
TABLE Test the DROP;
the CREATE TABLE Test (
ID int,
name varchar(20)
);

DROP TABLE test;
CREATE TABLE test (
id int NOT NULL,
name varchar(20) NOT NULL
);

DROP TABLE test;
CREATE TABLE test (
id int NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
PRIMARY KEY (id)
);

DROP TABLE test;
CREATE TABLE test (
id int NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
age int NOT NULL,
PRIMARY KEY (id)
);

DROP TABLE test;
CREATE TABLE test (
id int NOT NULL AUTO_INCREMENT ,
name varchar(20) NOT NULL,
age int NOT NULL,
info text NOT NULL,
PRIMARY KEY (id)
);

The DROP TABLE Test;
the CREATE TABLE Test (
ID int the NOT NULL the AUTO_INCREMENT UNIQUE,
name VARCHAR (20 is) the NOT NULL the DEFAULT 'name',
Age int the NOT NULL the DEFAULT 0,
info text the NOT NULL,
a PRIMARY KEY (ID)
); three, PHP related knowledge:
1.PHP form processing:
form processing code I created in the same page, as follows:
<HTML>
<head>
</ head>
<body>
<form Method, = "POST" Action = ""> // acetion expressed _self empty
<input type = "text" name = "id" value = ""> // query input box
<input type = "submit" name = "select" value = " query"> // query submit button
</ form>
<div> // PHP will run the results appear here! ! ! !
<? PHP
IF (! empty ($ _ POST [ "

the else {}
echo "You have not entered data !!!"
}

?>
</ Div>
</ body>
</ HTML>

2.PHP database connection:
code is as follows:
? <PHP
// use mydqli_connect connected,
$ myConn = mysqli_connect ( "localhost", "the root", "password", "the Test");
// mysqli_connect (String $ hostname, $ the User String, String password, String dbname)
// connection properties can also be written in php.ini, use ini_get (property name) way to read
// in php.ini has the following properties:
//mysqli.default_host=
//mysqli.default_user=
//mysqli.default_pw or = mysqli.default_password
//mysqli.default_port=
// If we add the attribute in php.ini can directly with mysqli_connect () is connected,
mysqli_select_db ($ myConn, "db_name"); // change the connection database, the database mysqli_connect if not connected, the operation needs to be selected using this method.
IF (! $ where myconn) {
Die ( "ERROR."

echo "the connection is successful!";
}
>
3.PHP processing query results:
<PHP?
// Suppose we create a test database test in the above table, the same table during the test structure and practice table creation.
// generates sql query
$ sql;
IF (! Empty ($ _ the POST [ "ID"])) {
$ sql = "the SELECT * the FROM Test the WHERE ID = $ _ the POST [ 'ID']";
} the else {
Die ( " Please enter a query value !! "); // end of program! !
}
// database connection
$ myConn = mysqli_connect ( "localhost", "the root", "password", "Test");
// judged
if ($ myConn) {// Here connection is successful!
// execute the query:
IF ($ Result = mysqli_querry (myConn $, $ SQL)) {
// read result
$ row_assoc = mysqli_fetch_assoc ($ result) ; // form the row of the result set using the associated array reading php
$ row_array = mysqli_fetch_array ($ result) ; // use the result set to form a row in an array of digital read php,

* The default, mysqli_fetch_array () to read in two ways
prototype * method is as follows:
* Mixed mysqli_fetch_array (Result mysqli_result $ [, $ int = resultType MYSQLI_BOTH])
* MYSQL_NUM,: indicates the read array mode digital
* MYSQL_ASSOC: in associative array reads
* MYSQL_BOTH: reading in two ways
* after reading one line of the embodiment, the result set will cursor over the next row, after the result set if only one row, it will read the cursor They will point to the end
*
* /
// output
// direct printing array:
print_r ($ row_assoc);
// formatted print:
the printf ( "ID:% S <br> name: Age <br>% S:% S < br> info:% s <br> " , $ row_assoc [" id "], $ row_assoc [" name "], $ row_assoc [" age "], $ row_assoc [" info "]);

the else {}
Die ( "query failed:." mysqli_error ($ myConn))
}
mysqli_close ($ myConn);
} // following the else {connection information indicates unsuccessful!
Die ( "error:" mysql_error ($ myConn).) ;
}
>
4.PHP database insert:
that the sentence be changed to insert data and query data and query data, which processes and use are similar.
<? PHP
// Suppose we create a test database test in the above table, the same table during the test structure and practice table creation.
// Suppose we create four text fields in the form, name attribute are the above mentioned id, name, Age, info
// generate sql insert statement
$ sql;
!! IF (empty ($ _ POST [ "the above mentioned id"]) && empty ($ _POST [ "name"]) &&! empty ($ _ the POST [ "Age"]) &&! empty ($ _ the POST [ "info"])) {
$ SQL = "the INSERT the INTO Test (ID, name, Age, info ) VALUES ($ _ POST [ ' id'], $ _ POST [ 'name'], $ _ POST [ 'age'], $ _ POST [ 'info']) ";
the else {}
Die ( "Please enter a query value !!"); // End of program! !
}
// database connection
myConn mysqli_connect = $ ( "localhost", "the root", "password", "Test");
// judged
if ($ myConn) {// Here connection is successful!
// Perform insert:
IF (mysqli_querry ($ myConn, $ SQL)) {
echo "inserted successfully!";
} The else {
Die ( "insert failed:." Mysqli_error ($ myConn))
}
mysqli_close ($ myConn);
?>
four, PHP security-related knowledge:
when using PHP operation of the database, there are several security-related methods, this method will not learn. The use of the above knowledge, you can quickly build a very simple SQL injection practice platform. As for the specific form of the platform, look at my hobbies, casual design. Complex, it may take a little HTML-related knowledge.
----------------
Disclaimer: This article is CSDN blogger "vent special children" in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/qq490765184/article/details/81841000

Guess you like

Origin www.cnblogs.com/ssc168/p/11569633.html