pdo

sql pdo php where in variable

大城市里の小女人 提交于 2021-02-11 18:11:18
问题 I'd like to select some data from my database using variables. Here is a short version of my code: // $a is either null or something like [1, 2] if ($a) { $debug = implode(',', $a); } else { $debug = [0-9]; } $sql = "SELECT id FROM user WHERE id IN ($debug)" How can I achieve that I get only user 1 and 2 (= value in $a ) if $a is set and all user if $a is not set? 回答1: First : be aware when you directly inject string inside queries, because you can be target of a SQL Injection Second : change

sql pdo php where in variable

こ雲淡風輕ζ 提交于 2021-02-11 18:08:11
问题 I'd like to select some data from my database using variables. Here is a short version of my code: // $a is either null or something like [1, 2] if ($a) { $debug = implode(',', $a); } else { $debug = [0-9]; } $sql = "SELECT id FROM user WHERE id IN ($debug)" How can I achieve that I get only user 1 and 2 (= value in $a ) if $a is set and all user if $a is not set? 回答1: First : be aware when you directly inject string inside queries, because you can be target of a SQL Injection Second : change

PHP Exception::getCode() contradicts Throwable interface that it implements

浪子不回头ぞ 提交于 2021-02-11 15:37:29
问题 I've found a contradiction I could not understand. Exception::getCode() has this definition: final public Exception::getCode ( void ) : mixed with description: Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException) but the Exception class implements Throwable interface that defines: abstract public getCode ( void ) : int So how for an example PDOException as a descendant of Exception could return string

PHP Exception::getCode() contradicts Throwable interface that it implements

僤鯓⒐⒋嵵緔 提交于 2021-02-11 15:36:14
问题 I've found a contradiction I could not understand. Exception::getCode() has this definition: final public Exception::getCode ( void ) : mixed with description: Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException) but the Exception class implements Throwable interface that defines: abstract public getCode ( void ) : int So how for an example PDOException as a descendant of Exception could return string

Explanation of PDO IN clause using prepared statements

我与影子孤独终老i 提交于 2021-02-11 15:21:29
问题 I am reading this answer: PHP - Using PDO with IN clause array It uses the code: $in_array = array(1, 2, 3); $in = str_repeat('?,', count($in_array) - 1) . '?'; $sql = "SELECT * FROM my_table WHERE my_value IN ($in)"; $stm = $db->prepare($sql); $stm->execute($in_array); $data = $stm->fetchAll(); Can anyone explain why they used $in = str_repeat('?,', count($in_array) - 1) . '?'; instead off: $in = str_repeat('?,', count($in_array)); I am puzzled and cannot figure out the logic behind the

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

Array in the parameter when creating PDO query

与世无争的帅哥 提交于 2021-02-09 20:34:22
问题 Can I use a few lines in the parameter Example SELECT id, city_id FROM an_objects WHERE city_id IN (:arrCity) (:arrCity) (1,2,3,4,5,6) But now I have done like this SELECT id, city_id FROM an_objects WHERE city_id IN (:1p, :2p, :3p, ...... :100p) And it's very bad 回答1: <?php private function PDOBindArray(&$poStatement, &$paArray){ foreach ($paArray as $k=>$v) { @$poStatement->bindValue($k, $v[0], $v[1]); } } // the array structure should now look something like this $inputArray = array( '

Array in the parameter when creating PDO query

爱⌒轻易说出口 提交于 2021-02-09 20:33:30
问题 Can I use a few lines in the parameter Example SELECT id, city_id FROM an_objects WHERE city_id IN (:arrCity) (:arrCity) (1,2,3,4,5,6) But now I have done like this SELECT id, city_id FROM an_objects WHERE city_id IN (:1p, :2p, :3p, ...... :100p) And it's very bad 回答1: <?php private function PDOBindArray(&$poStatement, &$paArray){ foreach ($paArray as $k=>$v) { @$poStatement->bindValue($k, $v[0], $v[1]); } } // the array structure should now look something like this $inputArray = array( '

Array in the parameter when creating PDO query

99封情书 提交于 2021-02-09 20:32:58
问题 Can I use a few lines in the parameter Example SELECT id, city_id FROM an_objects WHERE city_id IN (:arrCity) (:arrCity) (1,2,3,4,5,6) But now I have done like this SELECT id, city_id FROM an_objects WHERE city_id IN (:1p, :2p, :3p, ...... :100p) And it's very bad 回答1: <?php private function PDOBindArray(&$poStatement, &$paArray){ foreach ($paArray as $k=>$v) { @$poStatement->bindValue($k, $v[0], $v[1]); } } // the array structure should now look something like this $inputArray = array( '