DB other operation

A prepared statement is a feature used to execute the same/similar SQL statement repeatedlly with high efficiency.

Prepared statement basically work like this:

  Prepared: An SQL statement template is created and sent to the database.Certain values are left unspecified, called parameters(?)

  The database parses, compiles, and performs query optimization on the SQL statement template, and stores the result without executing it.

  Execute: At a later time, the application binds the values to the parameters, and the database executes the statement.The application may execute the statement as many times as it wants with differenet values.

Compared to executing SQL statements directly, prepared statements have 2 main advantages:

  Prepared statements reduces parsing time as the preparation on the query is done only once

  Bound parameters minimize bandwidth to the server as you need send only the parameters each time, and not the whole query

  Prepared statements are very useful against SQL injections, because parameter values, which are transmitted later using a different protocol, need not be correctly escaped.If the original statement template is not derived from external input, SQL injection cannot occur.

 

<?php

  $servername = "localhost";

  $username = "username";

  $password = "password";

  $dbname = "myDB";

  

  $conn = new mysqli($servername, $username, $password, $dbname);

  if($conn -> connect_error){

    die("Connection failed:" . $conn -> connect_error);

  }

   

  $stmt = $conn ->prepare("INSERT INTO MyTable(firstname, lastname, email) VALUES (?, ? , ?)");

  <!-- the first paramters tells the database what the parameters are sss means three parameters are all string type  -->

  <!--       i --integer    d -- double     s--string     b--BLOB        -->

  $stmt ->bind_parem("sss", $firstname, $lastname, $email);

  

  $firstname = "John";

  $lastname = "Doe";

  $email = "[email protected]";

  $stmt -> execute();

  $firstname = "Mary";

  $lastname = "Moe";

  $email = "[email protected]";

  $stmt -> execute();

   

  $stmt -> close();

  $conn -> close();

?>

<?php

  $servername = "localhost";

  $username = "username";

  $password = "password";

  $dbname = "myDBPDO";

  

  try{

    $conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);

    $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  

    $stmt = $conn ->prepare("INSERT INTO MyTable(firstname, lastname, email) VALUES(:firstname, :lastname, :email)");

    $stmt ->bindParam

  }catch(PDOException $e){

    error "Errpr: " .$ e -> getMessage();

  }

  

  $conn = null;

?>

<?php

  $servername = "localhost";

  $username = "username";

  $password = "password";

  $dbname = "myDB";

  

  $conn = new mysqli($servername,  $username, $password, $dbname);

  if($conn -> connect_error){

    die("Connection failed:" . $conn -> connect_error);

  }

  if($result -> num_rows > 0){

    while($row = $result -> fetch_assoc()){

      echo "id:" .$row["id"]. "- Name:" . $row["fistname"] . " " .$row["lastname"] . "<br>";

    }

  }else{

    echo "0 results";

  }

  $conn -> close();

?>

时间: 2024-10-20 15:22:08

DB other operation的相关文章

(翻译)《Hands-on Node.js》—— Why?

事出有因 为何选择event loop? Event Loop是一种推进无阻塞I/O(网络.文件或跨进程通讯)的软件模式.传统的阻塞编程也是用一样的方式,通过function来调用I/O.但进程会在该I/O操作结束前卡住不动,下面的这段伪代码可以演示阻塞I/O的情况: var post = db.query('SELECT * FROM posts where id = 1'); // 这行后面的指令都无法执行,除非等到这行指令执行完毕 doSomethingWithPost(post); do

mongodb - 查看正在执行的操作

查看正在执行的操作 db.currentOp() 查看系统执行的操作 db.currentOp(True) kill正在执行的操作 db.killOp(<operation id>)

Redis-string类型操作命令

APPEND key value 如果key已经存在,并且为字符串,那么这个命令会把value追加到原来值的末尾.如果key不存在,首先创建一个空字符串,再执行追加操作. 返回值:返回APPEND后字符串的长度. EXISTS mykey 0 APPEND mykey "Hello" 5 APPEND mykey " world" 11 GET mykey Hello world SETBIT SETBIT key offset value 对key所存储的字符串值

Transactional ejb 事务陷阱

对应ejb,默认是对整个类使用事务.所以所有方法都开启事务. 而对于用TransactionAttribute注释来引用容器管理的事务,只能在第一级的方法中使用.对应类中的方法再调用其它类中方法,注释事务就不起作用了. 代码示例如下: 在一个没有事务的类中调用: @TransactionAttribute(javax.ejb.TransactionAttributeType.NEVER)@Stateless public class MyTask{ @Inject Task t; t.doTas

Redis命令学习-string类型操作

APPEND key value 如果key已经存在,并且为字符串,那么这个命令会把value追加到原来值的末尾.如果key不存在,首先创建一个空字符串,再执行追加操作. 返回值:返回APPEND后字符串的长度. EXISTS mykey 0 APPEND mykey "Hello" 5 APPEND mykey " world" 11 GET mykey Hello world SETBIT SETBIT key offset value 对key所存储的字符串值

Redis 操作帮助类

首先从Nuget中添加StackExchange.Redis包 1.Redis连接对象管理帮助类 using Mvc.Base; using Mvc.Base.Log; using StackExchange.Redis; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; namespace Redis

The operation could not be performed because OLE DB provider &quot;SQLNCLI11&quot; for linked server &quot;server_name&quot; was unable to begin a distributed transaction.

Question: insert into #tmp exec usp_xxxx 报错 The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "server_name" was unable to begin a distributed transaction. Answer: 将linkserver Enable Promotion of

【MongoDB】The basic operation of Mongodb, Insert\Query\Delete\Update

1, Insert MongoDB is database storing document object, the type of which is called Bson.(like JSON); Example:  // document defination Now after using command[db.posts.insert(doc)], you will insert record successfully if seeing the The following pictu

奇怪的问题:db.tbLog.Add(log)竟然被覆盖了

tbLog log = new tbLog(); log.Operator_name = "test"; //1会员,2服务中心,3管理员 log.Operator_Type = "管理员"; log.DateTime = DateTime.Now; log.IP = GetClientIPOrAdd.GetIP(); log.Operation = "ExecuteSqlCommand时间:" + a; db.tbLog.Add(log); t