PHP operation MongoDB basic tutorial (connection, addition, modification, deletion, query)
The code is as follows://Connect to localhost:27017$cOnn= new Mongo(); //Connect to the default port of the remote host$cOnn= new Mongo(‘test.com’); //Connect to remote host port 22011$cOnn= new Mongo(‘test.com:22011’); //MongoDB has username and password$cOnn= new Mongo(“mongodb://${username}:${password}@localhost”) //MongoDB has a username and password and specifies the database blog$cOnn= new Mongo(“mongodb://${username}:${password}@localhost/blog”); //Multiple servers$cOnn= new Mongo(“mongodb://localhost:27017,localhost:27018”);//Select database blog$db = $conn-> blog;//Formulate the result set (table name: users)$collection = $db->users;//Newly added$user = array(‘name’ => ‘caleng ‘, ’email’ => ‘admin#admin.com’);$collection->insert($user);//Modify$newdata = array(‘$set’ => array (“email” => “[email protected]”));$collection->update(array(“name” => “caleng”), $newdata);//delete $collection->remove(array(‘name’=>’caleng’), array(“justOne” => true));//Find$cursor = $collection->find();var_dump($cursor);//Find a $user = $collection->findOne(array(‘name’ => ‘caleng’), array(’email’)); var_dump($user);//Close the database$conn->close();
Db operation example analysis of Thinkphp5.0 framework [connection, addition, deletion, modification, chain operation, etc.]
The example in this article describes the Db operation of the Thinkphp5.0 framework. Share it with everyone for your reference, as follows: Connection Actions: ‘mysql’, ‘hostname’ => ‘127.0.0.1’, ‘database’ => ‘bodywork3’, ‘username’ => ‘root’, ‘password’ => ‘666’, ‘hostport’ => ‘3306’, ‘charset’ => ‘utf8’ ]); //Method 3: Manual configuration, using a string $res = Db::connect(“mysql://root:[email protected]:3306/database_name#utf8”); //Method 4: Manual configuration, using a string //In this way, you need to have tp_db_config array configuration in the config configuration file $res = Db::connect(“tp_db_config”); } } Query sql: $data = Db::query(“select * from user”); dump($data); $data = Db::table(‘user’)->select(); $data = Db::table(‘user’)->find(); // Get a field value of a record $name = Db::table(‘user’)->where([‘id’]=>5)->value(‘name’); //Get a list of values $all_name = Db::table(‘user’)->column(‘name’); // Get a column of values, the second parameter is used as the index $all_name = Db::table(‘user’)->column(‘name’,’id’); //Notice: //select and column return an empty array when no data can be obtained. //find and value return null when the data cannot be obtained. $data = db(‘user’)->select();//The class will be instantiated every time $data = db(‘user’,[],false)->select();//The class will not be instantiated every time //Table name, use table $data = Db::table(‘prefix_user’)->select(); //Table name, use name $data = Db::name(‘user’)->select(); //When using table(), you need to bring the table prefix,…
Addition, deletion, modification and query operations of php data access
Add, delete, modify and check small exercises, let’s practice it 1. Check the news page —– main page View news id title author source content date update delete query($sql); $arr=$result->fetch_all(); foreach ($arr as $v) { echo ” {$v[0]} {$v[1]} {$v[2]} {$v[3]} {$v[4]} {$v[5]} update delete “; } ?> distribute news 2. Publish the news page —– add content Release news Title: Author: Source: content: Processing after submitting content: query($sql); if ($result) { header (“location:xinwen.php”); } else { echo “Failed to add news!”; } Third, delete content processing query($sql); if ($result) { header (“location:ChaKan.php”); } else { echo “Failed to delete data”; } ?> the 4. Modify the news page—-submit for viewing after modifying the content of the news Edit news query($sinfo); $arr = $r->fetch_row(); //All information about this person ?> <input type="hidden" name="newsid" value="”> Title: <input type="text" name="title" value="”> Author: <input type="text" name="author" value="”> Source: <input type="text" name="source" value="”> content: the Process after submitting the modified content: query($sql); if ($result) { header (“location:Update.php”); } else { echo “Failed to modify data!”; } The above is the whole content of this article, I hope it will be helpful for everyone to learn php programming.
Addition, deletion, modification and query of ThinkPHP database operation
Below is the thinkphp tutorial column to introduce the addition, deletion, modification and query of ThinkPHP database operations. I hope it will be helpful to friends in need! Basic use You can directly use the database to run native SQL operations, support query (query operation) and execute (write input operation) method, and supports parameter binding. Db::query('select * from think_user where id=?',[8]); Db::execute('insert into think_user (id, name) values (?, ?)',[8,'thinkphp']); Also Support named placeholder bindings, for example: Db::query('select * from think_user where id=:id', ['id'=>8]); Db::execute('insert into think_user (id, name) values (:id, :name)',['id'=>8,'name' ;=>'thinkphp']); You can use multiple database connections, use Db:: connect($config)->query('select * from think_user where id=:id',['id'=>8]); config is A single database configuration, supports arrays and strings, and can also be a configuration parameter name for a database connection. Query Data Basic Query Query a data use: // table method must specify a complete data table name Db::table('think_user')->where('id',1)->find(); The query result of the find method is not Exist, return null Query dataset use: Db::table( 'think_user')->where('status',1)->select(); The select method query result does not exist and returns an empty array If the data table prefix parameter is set, you can use Db:: name('user')->where('id',1)->find(); Db::name('user')->where('status',1)->select(); If your data table If the table prefix function…
Addition, deletion, modification and query of PHPTPinfo table
function info() { $model = D(“Info”); $ainfo = $model->field(“Info.Code as code,Info.Name as name,sex,Nation.Name as nationname,birthday” )->join(“Nation on Info.Nation = Nation.Code”)->select(); $this->assign(“info”,$ainfo); $this->display(); } info.html Code name Name Gender Ethnicity birthday Operation <foreach name=”info” item=”v”> <{$v.code}> <{$v.name}> <{$v[“sex”]?””male”:”female”}> <{$v.nationname}> <{$v.birthday}> $v.code}>”>Modify $v.code}>”>Delete </foreach> class=”a”>Add function Test() { if(empty($_POST)) { $nation=D(“Nation”); $attr=$nation->select(); $this->assign(“attr”,$attr); $this->display(); } else { $model=D(“Info”); $rules=array( array(“Code”,”require”,”Code cannot be empty”,0,”regex”,3), ); if(!$model->validate($rules)->create()) { echo $model->getError(); } else { $model->Sex=$_POST[“Sex”]==”1″?true:false ; $bs=$model->add(); //Jump page //1. Jump after success: success(“reminder”, “operation method of jump”, waiting time) //2. Jump after failure: error(“Prompt”, “Back to the previous page by default”) if($bs) { $this->success(“Added successfully”,”info”); } else { $this->error(“Failed to add”); } } } } test.html class=”a”> Code: Name: Gender: Male Female Ethnicity: <foreach name=”attr” item=”v”> <option value="<{$v.code}>”><{$v.name}> </foreach> Birthday: class=”b”> function YanZheng() { $model=D(“Info”); $str=””; $rules=array( array(“Code”,”require”,”Code cannot be empty”,0,”regex”,3), ); if(!$model->validate($rules)->create()) { $str=$model->getError(); } else { $str=”OK”; } $this->ajaxReturn($str,”eval”); } function XiuGai() { //Modify (save) $code=$_GET[“code”]; $model=D(“Info”); $nation=D(“Nation”); if(empty($_POST)) { $info=$model->find($code); $nations=$nation->select(); $names=$info[“nation”]; $this->assign(“nation”,$nations); $this->assign(“info”,$info); $this->display(); } else { $model->create(); $bs=$model->save(); if($bs) { $this->success(“modified successfully”,”info”); } else { $this->error(“Modification failed”); } } } xiugai.html class=”a”> <input type="hidden" name="Code" value="<{$info.code}>” /> Name: <input type="text" name="Name" value="<{$info.name}>” /> Gender: <if cOndition=”$info.sex == 1″> Male…
php modifies mongo, php operates MongoDB basic tutorial (connection, addition, modification, deletion, query)
The code is as follows: //Connect to localhost:27017 $conn = new Mongo(); //Connect to the remote host by default Port $conn = new Mongo('test.com'); //Connect to remote host port 22011 $conn = new Mongo('test.com:22011'); //MongoDB has username and password $conn = new Mongo(“mongodb://${username}:${password}@localhost”) //MongoDB has a username and password and specifies the database blog $ conn = new Mongo(“mongodb://${username}:${password}@localhost/blog”); //multiple servers $conn = new Mongo(“mongodb://localhost:27017,localhost:27018”); //select database blog $db = $conn->blog; //Formulate result set (table name :users) $collection = $db->users; //New $user = array('name' => 'caleng', 'email' 39; => 'admin#admin.com'); $collection->insert($user); //modify $newdata = array('$set' => array(“email” => “test@test.com”)); $collection->update(array(“name” => “caleng”), $newdata); //delete $collection ->remove(array('name'=>'caleng'), array(“justOne” => true)); //Find $cursor = $collection->find(); var_dump($cursor); //Find one p> $user = $collection->findOne(array('name' => 'caleng'), array('email' 39;)); var_dump($user); //Close the database $conn->close();
Addition, subtraction, multiplication and division, area calculation, small cases of printing pyramids and processing methods of oblique pyramids [php entry]
In this case, html js php loop control is used, knowledge related to logical judgment: When reviewing the loop control language and for loop printing pyramid today, The displayed pyramid is always screwed up in the browser, and Firefox, IE, and Chrome display incorrectly. After checking, I think it should be a problem with the character set, but after modifying the character set, it still doesn’t work Yes, it was later discovered that the utf-8 font space problem occupies two characters of space, the following are the replacement methods of some spaces I collected: character width) half blank (1 character width) one blank (2 character width) narrow blank (less than 1 character width) ? You can use a name or number as a substitute for a space, the name must be lowercase, and the “;” at the end cannot be omitted. Click me to view the case
Addition, deletion and correction of js+php form
Addition, deletion and modification of js+php form The effect is shown in the figure below: {{uploading-image-727245.png( uploading…)}} html base page < Student Performance Management System Get student results Add student grades Student number Student Name Chinese Achievement Mathematics Achievement English Achievement Operation ← previous page 1 2 3 4 5 Next page→ Student Name: Chinese score: Math score: English score: Cancel OK Transfer data through ajax-ajax.js export{ post, get, ajax}function post(){ var argus =Object.assign({“method” :”post”},…arguments) var g = new Ajax(argus); g.init();g.type()}function get(){ var argus =Object.assign ({“method” : “get”},…arguments) var g = new Ajax(argus); g.init();g.type()}// function ajax(){// var a =new Ajax(…arguments);// a.init();// a.type();// }function ajax(){ var a = new Ajax(…arguments); a.init();a.type()} >class Ajax{ constructor({method,url,data,success,error}){ this.method = method; this.url = url; this.data = data; this.success = success; this.error = error; } init(){ var xhr = null; try{ xhr = new XMLHttpRequest; br> }catch{ xhr = new ActiveXObject(“XMLHTTP”); } return xhr; } type(){ var xhr= this.init() var querystring=”” if(this.data){ querystring = this.queryString(this.data); } if(this.method==”get”) { xhr.open(this.method,this.url+”?”+querystring,true); xhr.send(); }else{ xhr.open(this.method,this.url,true); xhr.setRequestHeader(‘content-type’ , “application/x-www-form-urlencoded”); xhr.send(querystring); } xhr.Onreadystatechange= ()=>{ if(xhr.readyState == 4){ if(xhr.status==200){ if(this.success){ this.success(xhr.responseText) } }else{ if(this.error){ this.error(“error”+xhr.status) } } } } } queryString( dataObj){ var str=”; for(var attr in dataObj){ str+=`${attr}=${dataObj[attr]}&` } return str.substring(0,str.length-1)…
Some operations of sparse matrices (multiplication, exponentiation, addition, transposition)_Can sparse matrices be multiplied with ordinary matrices_sundial dreams’ blog
I believe everyone knows the matrix, and some common operations, such as multiplication and addition, I believe everyone is familiar with it. Take matrix multiplication as an example. Is the conventional implementation of the computer just a three-layer for? long[][] sum = new long[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) for (int k = 0; k < l; k++) sum[i][j] += a[i][k] * b[k][j]; What about sparse matrices? First introduce the sparse matrix. To put it bluntly, most of the positions in the matrix are 0, and only a small part is not 0, just like the matrix below . . . . . . . . . . . . . . . . \\ 0 & 1 & 0 & 1 & 0\\ 0 & 2 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0\\ 0 & 4 & 0 & 0 & 1 \end{pmatrix}” class=”mathcode” src=”https://private.codecogs.com/gif.latex?%5Clarge%20%20%20%5Cbegin%7Bpmatrix%7D%200%20%26%200%20%26%200%20%26%200%20%26%200%5C%5C%200%20%26%201%20%26%200%20%20%20%26%201%20%26%200%5C%5C%200%20%26%202%20%26%200%20%26%200%20%26%200%5C%5C%200%20%20%20%26%200%20%26%201%20%26%200%20%26%200%5C%5C%200%20%26%204%20%26%200%20%26%200%20%26%20%20%201%20%5Cend%7Bpmatrix%7D”> In terms of storage, if there is a two-dimensional array in the conventional matrix storage method, a lot of space must be wasted, so we can use the adjacency list in graph theory…
Addition, subtraction, multiplication and division algorithms commonly used in BigDecimal, size comparison, and saving two decimal points_bigdecimal addition, subtraction, multiplication, and division operations keep two decimal places_wei_wenlong’s blog
Not much to say, let’s record it first. BigDecimal coins = new BigDecimal(“0”); BigDecimal one = new BigDecimal(“1”); BigDecimal two = new BigDecimal(“2”); coins = coins.add( two ); //coins plus 2 System.out.println( coins.doubleValue()); //output is 2.0 coins = coins.subtract( one ); //coins minus 1 System.out.println( coins.doubleValue()); //The output is 1.0 coins = coins.multiply( two ); //coins are multiplied by 2 System.out.println( coins.doubleValue()); //The output is 2.0 int i = coins .compareTo(BigDecimal.ZERO) ; //Judge whether coins is greater than 0 int k = coins .compareTo( one ) ;//If i = 0, it means they are equal; if i 0 means coins > one coins = coins.setScale(2,BigDecimal.ROUND_HALF_UP) //Save two decimal places Division is trickier: When the bigdecimal type does division, there is no problem when the result is an integer or a finite decimal. If the result cannot be divisible, an error will be reported for an infinite decimal Error code: Bigdecimal bd = num1.divide(c).setScale(6,ROUND_HALF_UP); Error message: Non-terminating decimal expansion; no exact representable decimal result” Error translation: infinite decimal expansion; no exact decimal result. Reason for error: temporarily unknown Code improvements: BigDecimal bd = num.divide(c,6,ROUND_HALF_UP);