pdostatement

PDOStatement in foreach loop php

浪尽此生 提交于 2021-02-11 08:21:49
问题 There is a following code: <?php include 'connection.php'; //$db is declared here. It's a PDO object. foreach ($db->query("SELECT * FROM names") as $row) { echo $row['firstname'] . $row['lastname'] . $row['postcode'] . '<br>'; } ?> The code works as expected, but I don't understand the logic behind it. I've read on php.net that PDO::query() returns a PDOStatement object as a result set. So teoretically, this part: $db->query("SELECT * FROM names") is a PDOStatement object. How does foreach

PDOStatement in foreach loop php

◇◆丶佛笑我妖孽 提交于 2021-02-11 08:21:44
问题 There is a following code: <?php include 'connection.php'; //$db is declared here. It's a PDO object. foreach ($db->query("SELECT * FROM names") as $row) { echo $row['firstname'] . $row['lastname'] . $row['postcode'] . '<br>'; } ?> The code works as expected, but I don't understand the logic behind it. I've read on php.net that PDO::query() returns a PDOStatement object as a result set. So teoretically, this part: $db->query("SELECT * FROM names") is a PDOStatement object. How does foreach

PDO::Query() returning false

旧城冷巷雨未停 提交于 2020-01-02 10:17:43
问题 I'm attempting to learn and use PDO in PHP. I've come across an issue in the query() method. I'm attempting to use $sth = $db->query("SELECT * FROM titles ORDER BY RAND() LIMIT 1"); to randomly select a title for a website, but for some reason, $sth is always false. It works when I use prepare() and execute() , but I'm trying to find what's wrong in query() . Here's my entire function that is being called: function getTitle($db) { if($db) { $db->exec("USE " . $dbsite); $sth = $db->query(

Calling a key of a json_decoded array return NULL

丶灬走出姿态 提交于 2019-12-25 05:33:12
问题 I am struggling to understand why certain cases that specific way to access the key in PDO works while when I tried myself, it didn't. For example, $sth = $this->dbh->prepare("UPDATE eq_question SET ". $user->field ."=? WHERE questID=?"); this PDO accessed an array's key (which was in a javascript object then posted and json_decoded in PHP) with $user->field , but when I tried to simulate the decoded array and use var_dump ($user->field) , I will receive NULL . Can anyone tell me why is it

List of PDOStatement::bindParam data_type parameters

人走茶凉 提交于 2019-12-18 12:34:30
问题 Is there a list describing all of the data_type parameters you can use in PDOStatement::bindParam() ? If none, what do you commonly use, and for what type of field ? According to PHP manual: data_type Explicit data type for the parameter using the PDO::PARAM_* constants. I know about the PDO::PARAM_INT and PDO::PARAM_STR . I've heard of PDO::PARAM_LOB but I wasn't sure how and when to use that, though. (for date ?) 回答1: From the documentation here: PDO::PARAM_BOOL (integer) Represents a

PHP PDOStatement: Fetch A Row, as the First Column as the Key of an Array

眉间皱痕 提交于 2019-12-18 11:45:23
问题 I am using PDOStatement to query the database. Whenever I get a returned row, I want it to be fetched into an array, with the $row[0] as the key, and the subsequent elements in the row as the values. I can, of course, write a combination of foreach loops and if conditionals to do the job, such as the below: private static function GetMySQLResult($dbname, $sqlString) { $dbh = self::ConstructPDOObject($dbname); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $result=array();

try catch block not working with pdo statement and foreach

耗尽温柔 提交于 2019-12-13 07:06:49
问题 Recently I've encouraged weird error in PHP code. It's like the inner try{}catch(){} block is omited when using PDOStatement object with foreach(){} . Here is my test code which triggers this behaviour: <?php /* // THIS WORKS AS EXPECTED, JUST UNCOMMENT try{ for($i = 0; $i < 3; $i++){ try{ echo "\nTHROWING EXCEPTION\n\n"; throw new Exception("AAAAAAAAAAAAAAAAAAAAAAAAA"); echo "EXCETION THROWN (SHOULD NOT SHOW)\n"; }catch(Exception $err){ echo "========= INNER CATCH =========\n"; echo $err-

Why PDO debugDumpParams truncate query

眉间皱痕 提交于 2019-12-10 15:13:40
问题 I found same question here but it unanswered, and i provide more simple example here, and try ask again... Code: <?php $dbh = new PDO('mysql:dbname=test;host=127.0.0.1', 'root'); $sth = $dbh->prepare(" SELECT ' Dumps the informations contained by a prepared statement directly on the output. It will provide the SQL query in use, the number of parameters used (Params), the list of parameters, with their name, type (paramtype) as an integer, their key name or position, and the position in the

PDOStatement::rowCount result when used after PDO::commit?

蹲街弑〆低调 提交于 2019-12-06 23:49:17
问题 In the MySQL docs, there is a note about using mysql_affected_rows after a transaction commit: http://php.net/manual/en/function.mysql-affected-rows.php Note: Transactions If you are using transactions, you need to call mysql_affected_rows() after your INSERT, UPDATE, or DELETE query, not after the COMMIT. However, there is no such note on the PDOStatement::rowCount doc: http://www.php.net/manual/en/pdostatement.rowcount.php Does this mean the commit will not affect the affected rows count

How can I store objects in a session in PHP?

僤鯓⒐⒋嵵緔 提交于 2019-12-06 23:43:21
问题 Hello I would like to store or save an object inside a session,using classes like SessionHandler or arrays $_SESSION,I've seen that it is possible if I serialize the object,and I don't want to lose the methods of that object instance.. I've seen seralizing it is possible but the objects what I want to store is created by PDOStatement::fetchObject() althought the instance class is "Users" I get this error: PDOException: You cannot serialize or unserialize PDO instances Why? It is not a PDO