The previous chapter describes the build and fill data for MySQL-view database in Wamp, which describes how to get the data in MySQL via PHP code.
First, the previous chapter left the problem, through the PHP code to create a database, as for the command to create, the author does not want to say, interested students can self-Baidu access to relevant information. Reference: http://www.cnblogs.com/jiangxiaobo/p/7089345.html
1, open e:/myserver.com/phptest/, and then create a new mysql_data.php file;
<?PHP$arr=Array(); $servername= "localhost";//Server domain name $username= "Root";//Manage Account $password= "123456";//Manage Passwords $dbname= "MyTest";//our new database name//create connection, connect database, Mysqli_connect () function, mysql_connect () not supported, will error $conn=Mysqli_connect($servername,$username,$password,$dbname); //can print out a description of $coon exist, get to the data Print_r(Json_encode ($conn)); if(!$conn){ die("Connection failed:".)Mysqli_connect_error()); }Else { Echo"Connection succeeded"; }?>
Open the browser, enter http://myserver.com/mysql_data.php; The page shows the data, then the PHP file is successfully connected to the database MySQL
2. Create DATABASE Mytest1
Add the code to the mysql_data.php as follows:
<?PHP$arr=Array(); $servername= "localhost";//Server domain name $username= "Root";//Manage Account $password= "123456";//Manage Passwords $dbname= "MyTest";//our new database name//create connection, connect database, Mysqli_connect () function, mysql_connect () not supported, will error $conn=Mysqli_connect($servername,$username,$password,$dbname); //can print out a description of $coon exist, get to the data Print_r(Json_encode ($conn)); if(!$conn){ die("Connection failed:".)Mysqli_connect_error()); }Else { Echo"Connection succeeded"; } //Create a database Mytest1 $sql= "CREATE DATABASE mytest1"; if($conn->query ($sql)){ Echo"Database Creation succeeded"; }Else{ Echo"Database creation Failed"; } //Disconnecting database Connections $conn-close ();?>
Refresh the http://myserver.com/mysql_data.php page, let the PHP code run, and then refresh the database MySQL page can be found:
Database Mytest1 created successfully.
3. After creating the database Mytest1 , you need to re-create a new file mysql_table.php, which is used to create the data table and the corresponding data fill.
<?PHP$arr=Array(); $servername= "localhost";//Server domain name $username= "Root";//Manage Account $password= "123456";//Manage Passwords $dbname= "Mytest1";//Create connection $conn=NewMysqli ($servername,$username,$password,$dbname); //Detecting Connections if($conn-connect_error) { die("Connection failed:".)$conn-connect_error ()); } //Create a data table using SQL $sqlTable= "CREATE TABLE userinfo (ID INT (6) UNSIGNED auto_increment PRIMARY KEY, FirstName VARCHAR () not NULL, LastName varchar (+) not NULL, email varchar ( +))"; if($conn->query ($sqlTable) ===TRUE) { Echo"Table UserInfo created successfully"; } Else { Echo"Create data Table Error:".$conn-error; } //Create a database $sqlData= "INSERT into UserInfo (firstname, LastName, email) VALUES (' cp ', ' Cookie ', ' [email protected] ');"; $sqlData= "INSERT into UserInfo (firstname, LastName, email) VALUES (' CP1 ', ' cookie1 ', ' [email protected] ');"; $sqlData= "INSERT into UserInfo (firstname, LastName, email) VALUES (' CP1 ', ' cookie2 ', ' [email protected]e.com ');"; if($conn->query ($sqlData)) { Echo"New record inserted successfully"; } Else { Echo"Error:".$sqlData. "<br>".$conn-error; } //Disconnecting database Connections $conn-close ();?>
To run mysql_table.php, on the Refresh phpMyAdmin Server page, you can see the corresponding database on the left:
Note: Running mysql_table.php repeatedly adds the same data to the database Mytest1. If you have a classmate who wants to insert research into multiple data, you can look at the preprocessing statements, refer to: http://www.runoob.com/php/php-mysql-prepared-statements.html
Next, start implementing the data in the data mytest with PHP code.
1, open e:/myserver.com/phptest/, and then create a new php_mysql.php file, connect our previous chapter set up a database mytest;
<?PHP$arr=Array(); $servername= "localhost";//Server domain name $username= "Root";//Manage Account $password= "123456";//Manage Passwords $dbname= "MyTest"; //Create a connection $conn=Mysqli_connect($servername,$username,$password,$dbname); //Detecting Connections if($conn-connect_error) { die("Database connection failed:".)$conn-connect_error ()); }Else{ Echo"Database connection Success <br/>"; } $sql= "SELECT ID, username, age, gender, city from UserInfo";//reading a specific property from the data table set UserInfo $result=Mysqli_query($conn,$sql);//find the property that needs to be read from the corresponding Database//mysqli_num_rows () function can return the number of rows of the target data in the data table set, greater than 0, indicating that one or more exist. if(mysqli_num_rows($result) > 0) { //the output data Mysqli_fetch_assoc () function returns the index of the current row and sets the array, if not returning false; while($row=Mysqli_fetch_assoc($result)) { //iterate through the index of the target row and print it sequentially $arreach=Array("id" =$row["id"], "item" + =Array("username" =$row["username"], "age" =$row["Age"], "gender" =$row["Gender"], "city" =$row["City"])); Array_push($arr,$arreach); } } Else { Echo"Data not being queried"; }
Print it out, or you can pass it to HTMLPrint_r(Json_encode ($arr)); //Disconnecting database Connections $conn-close ();?>
Run php_mysql.php, print out, the page can show the data in the database we set up before;
Of course, we can process the data and then pass the data to HTML, which I've already covered in the previous chapters, and not much to say here.
About the Wamp of HTML, PHP, MySQL three of the operation and contact has been basically finished.
About Wamp's HTML, PHP, MySQL operations and connections-PHP and MySQL (iv)