1, first briefly introduce our database,thinkphp database has a tp_user table, then there are four fields .... ID,username,password,sex
Our task today is to set the data out under the thinkphp. ( additions and deletions to check and change )
<?php
This class is automatically generated by the system and is intended for testing purposes only
Class Indexaction extends Action {
Public Function Select () {
/* This is reading data from the database
* There is a select (); Find (); GetField () Three methods
$m =new Model (' user ');/* with $m=m (' user '), same effect without new, meaning that the User table in the database is instantiated , we can use it because the database table prefix is configured in config.php, so It is possible to write the user table directly. */
$m =m (' user ');//Use the model method to invoke the User table in the database
$arr = $m->select ();//select () method is to display all the information in the database, equivalent to $sql =select * from ' user ';
$arr = $m->find ();/* Returns only one piece of data, the default is the first one, and can display additional information when the parameter is passed in
$sql =select * from ' user ' limit 1;*/
$arr 1= $m->where (' id=2 ')->getfield (' username '),/*getfield () is used to get the field but only one is obtained, the first is the default, and the Where () method is added earlier. The parameters are written in terms of the condition, equivalent to
$sql =select ' username ' from ' user ' where ' id ' =2, limit 1;
*/
Reading of the data
$this->assign (' data ', $arr);//assign originally meant to be assigned, assign () method passes data to the foreground display
$this->display ();
}
}// Backstage section end
Front Display section:
1. If the content is only a single information in a field, because $this->assign (' data ', $arr), and data is assigned to it, then <{$data}> directly in the foreground; You can display the content.
2. If the set is in the form of an array, that is, the entire field is transferred out, $this->assign (' data ', $arr);d ata is an array form, then in the foreground traversal generally used <foreach > and <volist> Traversal, use the following:
<volist name= ' data ' id= ' VO ' >
<{vo.username}>--<{vo.password}>--<{vo.sex}>
</volist>// parsing:vo is the new name of this array in the foreground, that is , id=', this ID value must be taken. Vo. field name to be able to extract the contents of this field in the query.
<foreach> Basically the same is the id=' vo ', changed to item=' Vo ',
<foreach name= ' data ' item= ' VO ' >
<{vo.username}>--<{vo.password}>--<{vo.sex}>
</foreach>
End of reception display
/* This is to add information to the database
$m =m (' User ');//Using the model method is to use the database
$m->username= ' John Doe ';//assign username to a value
$m->sex=1;// the sex assignment .
$m->add ();//add (), is the Add method that comes with it, if you assign this statement to a variable, the return value is his ID number.
$this->display ();
*/
/* Delete Data
$m =m (' User ');
$m->where ("Sex=1 && username= ' King II '")->delete ();//The return value is the number of rows affected
equivalent to $sql ="Delete from ' user ' where ' sex ' =1 && ' username ' = ' King II '";
$this->display ();
*/
/* Modify Data
$m =m (' User ');
$data [' id ']=1;
$data [' username ']= ' ysj ';
$count = $m->save ($data);//If you assign this statement to a variable echo, it will return an effect row number (that is, affect a few lines, a person is a row)
if ($count >0) {
echo "Data added successfully";
}else{
echo "Data add Failed"
}
the above SQL statement is equivalent to $sql ="UPDATE ' tp_user ' SET ' username ' = ' ysj ' WHERE (' id ' = 7)";
*/
This is how thinkphp