pdo

PHP trying to use autoload function to find PDO class

烂漫一生 提交于 2020-01-13 09:49:06
问题 This has been bugging me for some time now and I can't seem to make sense of it. My phpinfo reports that PDO is installed and I can connect to my database on my index.php file. But when I try to open a PDO connection on a namespaced class, php is trying to use my autoload function to find PDO.php which won't work. My class is as follows: abstract class { protected $DB; public function __construct() { try { $this->DB = new PDO("mysql:host=$host;port=$port;dbname=$dbname", $user, $pass); }

View and debug prepared PDO query without looking at MySQL logs

烂漫一生 提交于 2020-01-13 05:24:30
问题 I want to see what PDO is preparing without looking into the MySQL logs. Basically the final query it has built right before it executes the query. Is there a way to do this? 回答1: There is no built-in way to do it. bigwebguy created a function to do it in one of his answers: /** * Replaces any parameter placeholders in a query with the value of that * parameter. Useful for debugging. Assumes anonymous parameters from * $params are are in the same order as specified in $query * * @param string

MySQL PDO - Setting Default Fetch Mode?

随声附和 提交于 2020-01-13 03:55:09
问题 So today, I'm finally making the transition from standard PHP MySQL functions to PDO. I noticed when fetching data as an object, we must run a line similar to the following: $STH = $DBH->query('SELECT name, addr, city from folks'); $STH->setFetchMode(PDO::FETCH_OBJ); $result = $STH->fetch(); My question is regarding line 2. Is there a way to set this as the default behavior so that we don't need to set the fetch mode every single time we wish to run a query? This seems pretty annoying to me.

How to fix Message: SQLSTATE[08004] [1040] Too many connections

 ̄綄美尐妖づ 提交于 2020-01-12 19:09:11
问题 I am using below code for database connection class Database extends PDO{ function __construct(){ try { parent::__construct(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME,DB_USER,DB_PASS); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); } catch(PDOException $e){ Logger::newMessage($e); logger::customErrorMsg(); } } } every thing like login , fetching data was working fine . Now suddenly I am having a exception

MySQL Update query - Will the 'where' condition respected on race condition and row locking? (php, PDO, MySQL, InnoDB)

这一生的挚爱 提交于 2020-01-12 18:49:44
问题 I am trying to build a first-come first-get model sale page. We have n number of items of the same type. We want to assign these n items to the first n users who made the request. Corresponding to each item, there is a database row. When the user presses buy button, the system tries to find an entry which is not yet sold ( reservationCompleted = FALSE ) and updates the user id and sets reservationCompleted to true. Since the database engine I am using is InnoDB, I understand that there is an

MySQL Update query - Will the 'where' condition respected on race condition and row locking? (php, PDO, MySQL, InnoDB)

人盡茶涼 提交于 2020-01-12 18:47:52
问题 I am trying to build a first-come first-get model sale page. We have n number of items of the same type. We want to assign these n items to the first n users who made the request. Corresponding to each item, there is a database row. When the user presses buy button, the system tries to find an entry which is not yet sold ( reservationCompleted = FALSE ) and updates the user id and sets reservationCompleted to true. Since the database engine I am using is InnoDB, I understand that there is an

Is it good to use htmlspecialchars() before Inserting into MySQL?

China☆狼群 提交于 2020-01-12 14:19:08
问题 I am a little confused on this. I have been reading about htmlspecialchars() and I am planning to use this for the textareas POST to prevent XSS attack. I understand that usually htmlspecialchars() are used to generate the HTML output that is sent to the browser. But what I am not sure is: 1) Is it a safe practice to use htmlspecialchars() to the user input data before I insert it into MySQL? I am already using PDO prepared statement with parameterized values to prevent SQL Injection. 2) Or,

How can I set the SQL mode while using PDO?

痞子三分冷 提交于 2020-01-12 03:31:48
问题 I am trying to set SQL modes, I can't figure out how to do it using PDO. I am trying to set Traditional mode in MySQL and not allow invalid dates. Can anyone help? 回答1: You can use the optional 'SESSION' variable when setting the sql_mode at runtime. That way it won't affect other clients. You can set the SESSION sql_mode and then set it back to the previous value after your query has completed. In this way, you can set the sql_mode for a specific operation. From the MySql manual: "You can

Mysql with Php 7

雨燕双飞 提交于 2020-01-11 14:55:27
问题 We are planning to upgrade our code base from PHP5 to PHP 7. I understand that mysql_* functions are deprecated in php 7. So do we have to rewrite all our code in mysqli_* or pdo? Is there any alternative for this? If not, which one would you suggest between mysqli_* and pdo? We have a huge code base, so changing all the code will be a colossal task, so we're looking for an alternative. 回答1: Well, you better clear that mess up first We are planning to upgrade our code base from PHP5 to PHP 7.

PDO Insert error on execute

谁说胖子不能爱 提交于 2020-01-11 14:41:28
问题 I can't find my mistake, I'm getting Execute fail error $db = new PDO('mysql:host=localhost; dbname=xxxxxx', 'yyyyyy', 'zzzzzz', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) or die ("fail");; $query = "INSERT INTO multiTicker (mtgox,btcstamp,btce,btcchina,myDateTime) VALUES (:mtgox,:btcstamp,:btce,:btcchina,:myDateTime)"; $st = $db->prepare($query) or die ("Query fail"); $st->execute(array(':mtgox' => $mtgox, ':btcstamp' => $btcstamp, ':btce' => $btce, ':btcchina' => $btcchina, '