pdo

PDO防止sql注入的原理

一笑奈何 提交于 2019-12-24 03:13:55
  首先,PDO可以被认作是一种通过编译SQL语句模板来运行sql语句的机制。   预处理语句可以带来两大好处:  1.查询只需要被解析(或编译)一次,但可以执行多次通过相同或不同的参数。当查询处理好后,数据库将分析,编译和优化它的计划来执行查询。对于复杂的查询这个过程可能需要足够的时间,这将显著地使得应用程序变慢,如果有必要,可以多次使用不同的参数 重复相同的查询。通过使用处理好的语句的应用程序避免重复 【分析/编译/优化】 周期。这意味着,预处理语句使用更少的资源,而且运行得更快。  2.绑定的参数不需要使用引号;该驱动程序会自动处理。如果应用程序使用预处理语句,开发人员可以确保不会发生 SQL注入 (但是,如果查询的其他部分使用了未转义的输入, SQL注入 仍然是可能的)。   预处理语句非常有用,PDO可以使用一种本地模拟的办法来为没有预处理功能的数据库系统提供这个功能。这保证了一个应用可以使用统一的访问方式来访问数据库。   使用PDO可以带来两个很好的效果,预编译带来查询速度的提升,变量的绑定可以预防 sql injection,其实PDO的预防 sql注入 的机制也是类似于使用 mysql_real_escape_string 进行转义。   PDO 有两种转义的机制:   (1)第一种是本地转义,这种转义的方式是使用单字节字符集(PHP < 5.3.6)来转义的(

PDO防sql注入原理分析

感情迁移 提交于 2019-12-24 03:13:27
使用pdo的预处理方式可以避免sql注入。 在php手册中'PDO--预处理语句与存储过程'下的说明: 很多更成熟的数据库都支持预处理语句的概念。什么是预处理语句?可以把它看作是想要运行的 SQL 的一种编译过的模板,它可以使用变量参数进行定制。预处理语句可以带来两大好处: 查询仅需解析(或预处理)一次,但可以用相同或不同的参数执行多次。 当查询准备好后,数据库将分析、编译和优化执行该查询的计划。对于复杂的查询,此过程要花费较长的时间,如果需要以不同参数多次重复相同的查询,那么该过程将大大降低应用程序的速度。通过使用预处理语句,可以避免重复分析/编译/优化周 期。简言之, 预处理语句占用更少的资源,因而运行得更快。 提供给预处理语句的参数不需要用引号括起来,驱动程序会自动处理。 如果应用程序只使用预处理语句,可以确保不会发生SQL 注入。 (然而,如果查询的其他部分是由未转义的输入来构建的,则仍存在 SQL 注入的风险)。 预处理语句如此有用,以至于它们唯一的特性是在驱动程序不支持的时PDO 将模拟处理。这样可以确保不管数据库是否具有这样的功能,都可以确保应用程序可以用相同的数据访问模式。 下边分别说明一下上述两点好处: 1.首先说说mysql的存储过程,mysql5中引入了存储过程特性,存储过程创建的时候,数据库已经对其进行了一次解析和优化。其次,存储过程一旦执行

Php - Pdo Ssh Tunnel

孤街浪徒 提交于 2019-12-24 02:59:15
问题 right now i am creating a ssh tunnel, so i can connect to my remote database, but for some reason the connection is still refusing... my script: try { $host = 'remote host'; $sshuser = 'ssh user'; $sshpass = 'ssh password'; $dbuser = 'db user'; $dbpass = 'db user'; $dbname = 'db name'; shell_exec("ssh -p$sshpass ssh -o StrictHostKeyChecking=no -f -L 3307:127.0.0.1:3306 $sshuser@$host"); $dbh = new PDO('mysql:host=127.0.0.1;port=3307;dbname=' .$dbname. '', $dbuser, $dbpass); $sth = $dbh-

read BLOB from mysql using php PDO

六眼飞鱼酱① 提交于 2019-12-24 02:55:07
问题 I have a database with a BLOB field (weeklyOccupancy). I am trying to access the data in PHP using: $sqlCmd = 'select weeklyOccupancy from Occupancy order by startDate;'; $pdoStmt = $dbh->query($sqlCmd); $pdoStmt->bindColumn(1, $lob, PDO::PARAM_LOB); $pdoStmt->fetch(PDO::FETCH_BOUND); foreach($pdoStmt as $row){ $weeklyData = stream_get_contents($lob); .... } However, stream_get_contents says that $lob is a string (named "Resource id #1) although I believe it should be a stream. I have seen

PDO connection to MySQL database refused

南楼画角 提交于 2019-12-24 02:39:06
问题 I am trying to connect to my NearlyFreeSpeech MySQL database. I can login through PHPMyAdmin but not through PDO. I am using this code $dbconn = new PDO('mysql:host=127.0.0.1;dbname='.$config['db'].'; port=3307', $config['user'], $config['pass']); Where $config is defined in a separate file. The error I get is: Warning: PDO::__construct() [pdo.--construct]: [2002] Connection refused (trying to connect via tcp://127.0.0.1:3307) Error: SQLSTATE[HY000] [2002] Connection refused and then

Fatal error: Call to a member function fetch() on a non-object?

ぐ巨炮叔叔 提交于 2019-12-24 02:33:12
问题 I am getting the follwing message returned to me Fatal error: Call to a member function fetch() on a non-object when running the following. It seems sporadic though, any thoughts? $cc_transaction_conn = new PDO('mysql:host='.$server.';dbname='.$db2use, $dbuser, $dbpass); $cc_transaction_query = $cc_transaction_conn->query("CALLgenInsCCTransactionLog('".$vmcnum."', ".$_SESSION[val].", 'Enrollment', 'Processor', 'Merchant', 'Auth', '".$card_expiration_month."', '".$card_expiration_year."', ".$

PDO - Invalid argument supplied for foreach()

江枫思渺然 提交于 2019-12-24 02:19:23
问题 I'm attempting to output the contents of my mysql database but no matter what method I use it errors, here is the code I'm using now; try { $dbh = new PDO("mysql:host = $hostname; dbname = kzkcubcy_webDev", $username, $password); /*** echo a message saying we have connected ***/ echo 'Connected to database<br />'; /*** The SQL SELECT statement ***/ $sql = "SELECT * FROM animals"; foreach ($dbh->query($sql) as $row) { print $row['animal_type'] .' - '. $row['animal_name'] . '<br />'; } /***

Inserting array data using PDO prepared statements [duplicate]

此生再无相见时 提交于 2019-12-24 01:54:17
问题 This question already has answers here : Binding multiple values in pdo (3 answers) Closed 2 years ago . Im slowly getting the hang of PDO and prepared statements, but could someone please clarify the following: I understand you can insert data from an array using the following code: $values = array('bill', 'ben', 'bob'); $stmt = $db->prepare("INSERT INTO first_page_data(first_name) VALUES(:fname)"); $stmt->bindParam(':fname', $first_name, PDO::PARAM_STR); foreach ($values as $first_name) {

Module pdo_mysql ini file doesn't exist under /etc/php/7.0/mods-available

六月ゝ 毕业季﹏ 提交于 2019-12-24 01:52:59
问题 I recently upgraded to PHP7, now I learned to find out out while connecting to Laravel, with this problem [PDOException] could not find driver.How am I going to configure or install it? Thank you very much. 回答1: Here is a solution: sudo apt-get install php7.0-mysql sudo phpenmod pdo_mysql sudo service apache2 restart 回答2: Following steps are working for me Installing php7.0-mysql sudo apt-get install php7.0-mysql You will then need to ensure the module is enabled: sudo phpenmod pdo_mysql Then

Should I use PDO PARAM_LOB or PARAM_STR for MySQL TEXT type?

a 夏天 提交于 2019-12-24 01:45:08
问题 Should I use PDO PARAM_LOB or PARAM_STR for MySQL TEXT type? I expect my data to be 5000+ characters. $stmt->bindParam(':notes', $notes, PDO::PARAM_STR); OR $stmt->bindParam(':notes', $notes, PDO::PARAM_LOB); 回答1: If you're going to be using large chunks of data, as you have mentioned in your use-case, then yes -- I would use PDO::PARAM_LOB to manipulate your data using data streams. According to the PHP documentation: At some point in your application, you might find that you need to store