pdo

Fatal error: Call to undefined method PDO::close()

*爱你&永不变心* 提交于 2019-12-24 07:34:57
问题 I have got that error and the line was this: public function __destruct() { $this->db->close(); } I use PDO, is this the problem, does the PDO driver not recognize this function? If yes, what is its equivalent in PDO? 回答1: A PDO connection is closed by destroying its object: The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the

my pdo connection doesn't work

做~自己de王妃 提交于 2019-12-24 07:13:20
问题 Echoing to my previous question about SQL-injection. I'm trying to set up a PDO connection. For that I want to replace my old code with the new: Here is the old $conn = mysql_connect("localhost", "sec", "dubbelgeheim") or die('Error: ' . mysql_error()); mysql_select_db("bookshop"); $SQL = "select * from productcomment where ProductId='" . $input . "'"; $result = mysql_query($SQL) or die('Error: ' . mysql_error()); $row = mysql_fetch_array($result); if ($row['ProductId']) { echo "Product:" .

Drupal db_select PDO Statement: ORDER BY CASE

拜拜、爱过 提交于 2019-12-24 06:58:08
问题 Hi I have written a MySQL statement that has a conditional order by clause which you can see in the example below. MySQL Example: SELECT title, description FROM books WHERE title LIKE "%keyword%" OR description LIKE "%keyword%" ORDER BY CASE WHEN title LIKE "%keyword%" THEN 1 ELSE 2 END I am now trying to recreate this statement in Drupal using its PDO style db_select() function. But I have got stuck when writing the ORDER BY clause. Drupal Example: $node_select = db_select('node', 'n');

How do I get meaningful error messages out of MySQL using PDO prepared statements?

送分小仙女□ 提交于 2019-12-24 06:46:18
问题 Suppose I have the following table: mysql> CREATE TABLE example ( -> `id` INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, -> `stuff` VARCHAR(255) NOT NULL -> ); Query OK, 0 rows affected (0.06 sec) The stuff column was declared as NOT NULL . When a null value gets passed to it in a prepared statement, it naturally fails. Thus: mysql> PREPARE test FROM "INSERT INTO example (stuff) VALUES (?)"; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> SET @a = NULL; Query OK, 0 rows affected

How to pass DB connection to another class?

∥☆過路亽.° 提交于 2019-12-24 06:36:08
问题 I'm currently trying to pass a DB connection as follows: class Test { public $user; public $db; function __construct() { // connect to database try { $this->db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DATABASE.'', DB_USERNAME, DB_PASSWORD); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $err) { die($err->getMessage()); } $this->user = new User($this->db); } } class User { public $db; function __construct($db) { $this->db = $db; } // execute some

Add a new column to an existing table in MySql using PHP with PDO

送分小仙女□ 提交于 2019-12-24 06:13:08
问题 So far I've got this code: $column_name = strtolower($_POST['<user input>']); if(!preg_match('/[^A-Za-z0-9.#\\-$]/', $column_name)){ if(!empty($column_name)){ $st = $db_pdo->prepare("DESCRIBE <table name>"); $st->execute(); $st = $st->fetchAll(PDO::FETCH_COLUMN); $compare = $st; foreach($compare as $key){ if($key === $column_name){ die('Project name already exists. Please select a different name.'); } } $st = $db_pdo->prepare("ALTER TABLE emails ADD <column name> varchar"); $st->execute(); }

Add a new column to an existing table in MySql using PHP with PDO

混江龙づ霸主 提交于 2019-12-24 06:13:06
问题 So far I've got this code: $column_name = strtolower($_POST['<user input>']); if(!preg_match('/[^A-Za-z0-9.#\\-$]/', $column_name)){ if(!empty($column_name)){ $st = $db_pdo->prepare("DESCRIBE <table name>"); $st->execute(); $st = $st->fetchAll(PDO::FETCH_COLUMN); $compare = $st; foreach($compare as $key){ if($key === $column_name){ die('Project name already exists. Please select a different name.'); } } $st = $db_pdo->prepare("ALTER TABLE emails ADD <column name> varchar"); $st->execute(); }

Security for prepared SQL statement with REGEX in query

二次信任 提交于 2019-12-24 05:53:25
问题 I'm trying to use a prepared PDO statement for use in a database query that looks like the following, where the placeholders variables are represented by the numbers in quotes, i.e., "123" and "456": SELECT `user_ID` as `ID` FROM `usermeta` WHERE (`meta_key` = 'custom_fields') AND (`meta_value` REGEXP '.*"ABC";.*s:[0-9]+:"123".*') AND (`meta_value` REGEXP '.*"DEF";.*s:[0-9]+:"456".*') My question is, would the best practice be to bind to the whole REGEX expression, or just the "123" and "456"

Security for prepared SQL statement with REGEX in query

南楼画角 提交于 2019-12-24 05:52:32
问题 I'm trying to use a prepared PDO statement for use in a database query that looks like the following, where the placeholders variables are represented by the numbers in quotes, i.e., "123" and "456": SELECT `user_ID` as `ID` FROM `usermeta` WHERE (`meta_key` = 'custom_fields') AND (`meta_value` REGEXP '.*"ABC";.*s:[0-9]+:"123".*') AND (`meta_value` REGEXP '.*"DEF";.*s:[0-9]+:"456".*') My question is, would the best practice be to bind to the whole REGEX expression, or just the "123" and "456"

SQLite PDO bindings not working?

假如想象 提交于 2019-12-24 05:18:16
问题 I feel like I'm losing my mind on this one. I have three simple tables. A user table, a roles table, and a role_user table that joins the user and roles in a many to many relationship. I have the following code to the roles for a user: $query = $pdo->prepare('select roles.* from roles inner join role_user on roles.id = role_user.role_id where role_user.user_id = ?'); $query->execute(array('1')); die(var_dump($query->fetchAll())); This returns an empty array. No results. However, if I change