Mainly include:
1, PHP to create databases and tables;
2, PHP to insert content into the database;
3, PHP from the database to read content to the foreground display;
The first is to create the database and tables:
<?php $servername = "localhost";//Database host $username = "root";//database user name $password = "Root";//Database Password $dbname = "Lyt";//The database name to be created// create a connection $conn = new mysqli ($servername,
$username, $password);
Detect connection if ($conn->connect_error) {die ("Connection error: " . $conn->connect_error);} Start creating database $sql = "Create database lyt default character set utf8 collate utf8_general_ci "//red for setting the database encoding format, many tutorials on the internet have missed this step, leading to all kinds of garbled phenomena if ($conn->query ($sql) == = true) {echo "Database creation succeeded <br/>";} else {echo Database creation failed:  . $conn->error;} try {$conn = new pdo ("
mysql:host= $servername;d bname= $dbname ", $username, $password);
$conn->setattribute (pdo::attr_errmode, pdo::errmode_exception); Create tables and fields $sql = create table myguests (//red is the table name, the following red part is the field of interior and exterior, also want to set the encoding, VARChar is the type of field that stores data Id int (6) unsigned auto_increment primary key, Product varchar (30) character set utf8 collate utf8_general_ci, Guestname varchar (m) CHARACTER set utf8 collate utf8_general_ci, Phone varchar (m) character set utf8 collate utf8_general_ci, Address varchar (m) character set utf8 collate utf8_general_ci, Message varchar ( character set utf8 collate utf8_)
General_ci, Reg_date timestamp) Default character set utf8 ";
$conn->exec ($sql);echo "Data table creation succeeded"; catch (pdoexception $e) {echo $sql . "<br>" . $e->getmessage ();}?>
This code into the PHP file, by accessing this PHP page can create a LYT database, the database has a myguests table, the table has Id,product,guestname,phone,address,message,reg _date field, but there is still nothing inside, we have to insert data into it, such as the customer filled out a form of data submitted to the database, you need to use the following PHP code:
<?php $servername = "localhost";//Database host $username = "root";//database user name $ password = "root";//Database Password $dbname = "Lyt";//Create a connection $conn the name of the database to be created// = mysqli_
Connect ($servername, $username, $password, $dbname); if (! $conn) {die ("Database connection failed: " . mysqli_connect_error ()); Mysqli_query ($conn, "set Names utf8 ")//red for setting the database encoding format, many tutorials on the internet have missed this step, resulting in a variety of garbled and error $sql = " insert into myguests (product, guestname, phone, address, message) values (' ". $_POST[' Pro ']." ', ' ". $_ post[' Gname ']. "', '". $_post[' phone '. "', '". $_post[' addr ']. "', '". $_post[' Mess ';
/Blue section is the name value of the form input, note that the form type must be post. if (Mysqli_query ($conn, $sql)) {echo "commit successfully";} else {echo "Error: " . $sql . "<br>" . mysqli_error ($
conn); }
Throw this code into a form.php file, as a form's processing file, by introducing the file into the form, such as:
<form name= "form" method= "POST" action= "form.php" >
When you submit the form, you insert the data that is filled in the form into the database. In addition to the form you can manually specify the insertion of some data, the above part of the code written dead on the line, such as:
VALUES (' haha ', ' hello ', ' I'm fine ', ' you? ', ' I'm not good! ') '; Through this step, the database is finally content. But the light has the content not to be able, how should I see this data? I will not go to the database to see it, or to the database content in front of the display to be convenient, then PHP from the database to read the data, the code is as follows:
$conn = mysql_connect ("localhost", "root", "root")//Connect to the database, please fill in your own username password
mysql_select_db ("Lyt");/Choose MySQL Database
mysql_query ("Set names UTF8")//set to UTF8 encoded
$result = mysql_query ("SELECT * from Myguests", $conn);//Execute SQL query directives, Myguests for Table name
echo "<table border=1 style= ' margin:0 auto; ' ><tr> ";
while ($field = Mysql_fetch_field ($result)) {//Use while output header
echo ' <td> '. $field->name. "</td>";
}
echo "</tr>";
while ($rows = Mysql_fetch_row ($result)) {//use while to traverse all records and display the
echo "<tr>" in the form's TR;
for ($i = 0; $i < count ($rows); $i + +)
echo "<td>". $rows [$i]. " </td> ";
}
echo "</tr></table>";
Throw this code into another PHP file, you can access the PHP file to the specified database, the contents of the database table presented in tabular form.
In fact, all three pieces of code used the same small block of code:
$servername = "localhost";//Database host
$username = "root";//database user name
$password = "root";//Database password
$dbname = "Lyt"; /The name of the database that will be created
This small piece of code can be saved as another PHP file, through the include ' data.php ', introduced, so that easy to modify.