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:password@127.0.0.1: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,…

SQLite operation class implemented by PHP based on PDO [including addition, deletion, modification, query and transaction operations]

This article describes the SQLite operation class implemented by PHP based on PDO. Share it with everyone for your reference, as follows: Direct code: Note: Be sure to write the database save path db = new PDO(‘sqlite:’.dirname(__FILE__).’\log.db’); $this->table_name=$tab; $this->tab_init(); } public function tab_init() { # Table initialization, create table $this->db->exec(“CREATE TABLE log( id integer PRIMARY KEY auto increment, urls varchar(200), ip varchar(200), datetimes datetime default (datetime(‘now’, ‘localtime’)) )”); } public function insert($tab_name, $key_list, $value_list) { // echo “INSERT INTO “.$tab_name.” (“.$key_list.”) values(“.$value_list.”)”; $result=$this->db->exec(“INSERT INTO “.$tab_name.” (“.$key_list.”) values(“.$value_list.”)”); if (!$result) { return false; } // echo “{{{INSERT INTO “.$tab_name.” (“.$key_list.”) values(“.$value_list.”)}}}}”; $res=$this->db->beginTransaction();//Transaction back to gun } public function total($tab_name,$tj=”)//find the total number of records { $sth = $this->db->prepare(‘SELECT count(id) as c FROM ‘.$tab_name.’ ‘.$tj); $sth->execute(); $result = $sth->fetchAll(); return $result[0][‘c’]; } public function update() { # Revise } function delete($value=”) { # delete } public function query($tab_name,$tj=”)//table name and condition { $sth = $this->db->prepare(‘SELECT * FROM ‘.$tab_name.’ ‘.$tj); // echo ‘SELECT * FROM ‘.$tab_name.’ ‘.$tj; $sth->execute(); $result = $sth->fetchAll(); return $result; } } // $db=new SqliteDB(); // $res=$db->insert(‘log’,’ip,urls,datetimes’,'”127.0.0.1″,”www.baidu.com”,”2012-12-12 00:00:00″ ‘);//add case // $res=$db->query(‘log’);//query case // $res=$db->total(‘log’);//Query case // print_r($res); // foreach ($res as $key => $row) { // echo $row[‘urls’]; //…

php product pagination, modification, deletion, etc.

php product pagination, modification, deletion, etc. Following the last php add product article, continue add.php Add product Product name Product Specifications Price Inventory Image URL URL Modify page modify.php Modify product Product name <input name="proname" id="proname" type="text" value="” /> Product Specifications <input name="proguige" id="proguige" type="text" value="” /> Price <input name="proprice" id="proprice" type="text" value="”/> Inventory <input name="proamount" id="proamount" type="text" value="” /> Image URL <input name="proimages" id="proimages" type="text" value="” /> URL <input name="proweb" id="proweb" type="text" value="” /> Delete del.php Let’s write a page fenye.php <?php //1. Connect to the database $link=@mysql_connect('localhost','root','root') or die('Database connection failed'); //2-1 select the database library: method one //mysql_query('use data') or die('Database connection failed'); // 2-2 mysql_select_db('phpmysql') or die('Database selection failed'); //Set client encoding mysql_query('set names gbk'); //retrieve data $s=mysql_query('select count(*) from products'); $rows=mysql_fetch_row($s);//Match resources into an index array $recordcount=$rows[0];//Total number of records //Define page size $pagesize=2; // Find the total number of pages $pagecount=ceil($recordcount/$pagesize); //Click on the page number to get the current page $pageno=isset($_GET['pageno'])?$_GET['pageno']:1;//Whether the get request has value, if there is value, use itself if ($pageno$pagecount){ $pageno=$pagecount; } // Get the starting position of the current page $startno=($pageno-1)*$pagesize; // Get the content of the current page $sql=”select * from products limit $startno,$pagesize”; $rs=mysql_query($sql); ?> Number…

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();

Personal center creation, modification, verification in PHP self-training project (server-side and client-side verification)

When the registration is successful and you log in, enter the personal center to view and modify your own information Step 1: Create a personal center: <?php //Define a constant to authorize calls to files in includes define(‘IN_TG’,true); //Define a constant to specify this The content of the page define(‘SCRIPT’,’member’); //Import public files require dirname(__FILE__).’/includes/common.inc.php’; // Is it normal to log in? if (isset($_COOKIE[‘username’])) { //Get Data $_rows=_fetch_array(“SELECT tg_username,tg_sex,tg_face,tg_email,tg_url,tg_qq,tg_level,tg_reg_time FROM tg_user WHERE tg_username='{$_COOKIE[‘username’] }'”); if($_rows){ /* $_username=$_rows[‘tg_username’]; $_sex=$_rows[‘tg_sex’]; */ $_html=array(); $_html[‘username’]=$_rows span>[‘tg_username’]; $_html[‘sex’] = $_rows[‘tg_sex’]; span>$_html[‘face’] = $_rows[‘tg_face’]; $_html[‘ email’] = $_rows[‘tg_email’]; $_html[‘url’] = $_rows [‘tg_url’]; $_html[‘qq’] = $_rows[‘tg_qq’]; $_html[‘reg_time’] = $_rows[‘tg_reg_time’]; switch ($ _rows[‘tg_level’]){ case 0: $_html[‘level’] = ‘Ordinary Member’; break; case 1: $_html[‘level’] = ‘administrator’; break; default: $_html[‘level’] = ‘Not a local administrator’; } $_html = _html($_html); }else{ _alert_back(‘This user does not exist!’); } } else{ _alert_back(“Illegal login!”); } ?> PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <?php require ROOT_PATH.’includes/title.inc.php’; ?> <?php require ROOT_PATH.’includes/header.inc.php’; ?> <?php require ROOT_PATH.’includes/member.inc.php’; ?> Member Management Center <dl> Username: <?php echo $_html[‘username’]?> Sex: <?php echo $_html[‘sex’]?> Avatar: <?php echo $_html[‘face’]?> Email: <?php echo $_html[’email’]?> Home Page: <?php echo $_html[‘url’]?> QQ :<?php echo $_html[‘qq’]?> Registration time: <?php echo $_html[‘reg_time’]?> Identity: <?php echo $_html[‘level’]?> </dl> <?php…

Unity3D performs addition, deletion, modification, and query on MySQL through PHP

Introduction We use Unity3D for database-related operations, usually the easiest way is to use php for connection. Fortunately, neither php nor MySQL is so difficult to master, so our front-end should understand this knowledge as much as possible. This article uses a case of game score submission to introduce the method of adding, deleting, modifying and checking scores on MySQL through php. SQL statements will be used in php scripts, which requires certain mastery. Start 1. MySQL section. Use WampServer to quickly build the server, create a new database “simpledata”, and create a table “highscores”, set the fields as follows; id name score 1 Anna 100 2 Pharah 200 2. PHP section. Create a new addscore.php in the Wamp default root directory (/www); Using action in php can correspond to the function of C# very well. error_reporting(E_ALL ^ ​​E_DEPRECATED); can help us remove redundant exceptions and make the Unity Console interface clean. <?phperror_reporting(E_ALL ^ ​​E_DEPRECATED); mysql_connect(“localhost”,”root”,””) or die(‘Could not connect: ‘ . mysql_error()); mysql_select_db(“simpledata”); //Increase if($_REQUEST[‘action’]==”submit_highscore”) { $ name = $_REQUEST[‘name’]; $score = $_REQUEST[‘score’]; $query = “INSERT INTO `highscores` (`name`,`score`) VALUES (‘$name ‘,’$score’)”; mysql_query($query); echo “Insert ” . $name . ” ” . $score; } //Delete, all if($_REQUEST[‘action’]==”delete_all_highscore”) { $query…

phpmysql addition, modification, deletion and query_PHP connects to MySQL to perform addition, deletion, modification, and query operations

This article mainly introduces the method of adding, deleting, modifying and checking PHP to MySQL ,has a good reference value,Let’s take a look with the editor Not much to say,Please see the code: code Name Gender Ethnicity Birthday 1. Create a mysqli object, create a connection object $db = new MySQLi(“localhost”, “username”, “password”, “database name”); 2. Prepare a SQL statement $sql = “select * from info”; 3. Execute the SQL statement, if it is a query statement, Successfully return the result set object $reslut = $db->query($sql); 4. Determine whether the return is successful if ($reslut) { while($attr = $reslut->fetch_row()) { echo “ {$attr[0]}{$attr[1]}{$attr[2]}{$attr[3]}{$attr[4]}”; } } ?> fetch_all() returns all arrays fetch_row() returns index array fetch_assoc() Returns an associative array fetch_object() returns an object fetch_array() returns an array with both an indexed, and an associated database deletion ,Add,Modify operation //Create connection object $db = new MySQLi(“localhost”,”username”,”password “,”database name”); //Prepare SQL statement $sql = “delete from info where code='p004' “; delete //$sql = “insert course values” increase //$sql = “update table name set field= information where field =information” change //execute SQL statement $r = $db->query($sql); if($r) { echo “executed successfully”; } else { echo “Execution failed”; } ?> More PHP connections to…

0ee0a7a08d4f1802db54bb819ab649a1.png

phpmysql delete, delete and add_PHP, MYSQLI to achieve simple addition, deletion, modification, and query functions (beginners)

PHP, MYSQLI implement simple addition, deletion, modification, and query functions (beginners) Release time:2020-07-29 18:21:23 Source :51CTO Read :65441 Author :Worriless you index.php #Connection Database p> $conn = mysqli_connect(“localhost”,”root”,””); #Judge whether the connection is successful if(!$conn) { echo “failure”; } //select database mysqli_select_db($conn,”bbs”); //Prepare sql statement $sql = “select * from bbs_user”; //Send sql statement $obj = mysqli_query($conn,$sql); echo “ “; echo “ echo “ Number Name Password Address Gender Age Operation”; while($row = mysqli_fetch_assoc($obj)){ echo “ “; echo &#39 ; '.$row['id'].''; echo ' '.$ row['username'].''; echo ' '.$row['password'] .''; echo ' '.$row['address'].''; echo ' '.$row['sex'].''; echo ' '.$row['age'].''; echo ' Delete/Modify'; echo “ “; } echo “ “; echo “Add”; echo “ “; //Close the connection mysqli_close($conn); ?> Click to delete : del.php $id &# 61;$_GET['id']; $link = mysqli_connect('localhost','root','root',' 39;'); if(!$link){ exit('Connection failed'); } mysqli_select_db($link,'bbs'); $sql = “delete from bbs_user where id =$id”; $result = mysqli_query($link,$sql); if($result && mysqli_affected_rows($link)){ echo ” Delete successfully return”; }else{ echo “Delete failed”; } mysqli_close($link); ?> Change data& #xff1a;<?php $id = $_GET['id']; $link = mysqli_connect(&#39 ;localhost','root',''); if(!$link){ exit(&# 39; Connection failed& # 39;); } mysqli_select_db($link,'bbs'); $sql & #61; “select * from bbs_user where id =$id”; $obj = mysqli_query($link,$sql); $row & #61; mysqli_fetch_assoc($obj); ?> ” />…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索