pdo

Excluding numbered-index elements of PDO::fetchAll()

别来无恙 提交于 2020-07-30 07:55:08
问题 $allrows = $pdo->fetchAll(); // select * from .... I want to transform this $allrows into JSON by doing : echo (json_encode($allrowl,JSON_PRETTY_PRINT)); My problem is that this fetchAll will not only extracting data as associate array but also indexed array for each element, hence repeating elements. [ { "org_id": "1", "0": "1", "category": "A", "1": "A", }, { "org_id": "2", "0": "2", "category": "A", "1": "A", } ] Thank you. 回答1: That's becuase the default fetch mode is FETCH_BOTH . CHange

Excluding numbered-index elements of PDO::fetchAll()

社会主义新天地 提交于 2020-07-30 07:54:28
问题 $allrows = $pdo->fetchAll(); // select * from .... I want to transform this $allrows into JSON by doing : echo (json_encode($allrowl,JSON_PRETTY_PRINT)); My problem is that this fetchAll will not only extracting data as associate array but also indexed array for each element, hence repeating elements. [ { "org_id": "1", "0": "1", "category": "A", "1": "A", }, { "org_id": "2", "0": "2", "category": "A", "1": "A", } ] Thank you. 回答1: That's becuase the default fetch mode is FETCH_BOTH . CHange

Convert PDO resultset to array of objects

梦想与她 提交于 2020-07-19 06:48:10
问题 I have a PHP class called Product: class Product { $id; $name; } And another class that get data from database: $stm = $this->dsn->prepare($sql); $stm->execute(); $rst = $stm->fetchAll(PDO::FETCH_ASSOC); How can I convert this PDO resultset ($rst) to an array of objects Product? 回答1: Use the PDO::FETCH_CLASS argument. class Product { public $id; public $name; } $stm = $this->dsn->prepare($sql); $stm->execute(); $result = $stm->fetchAll( PDO::FETCH_CLASS, "Product" ); http://php.net/manual/en

Oracle PHP PDO exception: could not find driver

蹲街弑〆低调 提交于 2020-07-16 05:01:36
问题 I'm trying to hack together a script to connect to a remote oracle database and execute a simple query Through extensive searches I found the following script: <? $tns = " (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = bogus.com.au)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = myDB ) ) ) "; $pdo_string = 'oci:dbname='.$tns; try { $dbh = new PDO($pdo_string, 'test', 'fake'); } catch (PDOException $e) { echo "Failed to obtain database handle: " . $e->getMessage(); exit;

Force exact string MATCH for PDO prepared statements

喜欢而已 提交于 2020-07-03 10:04:49
问题 I have recently adopted PDO prepared statements. When writing WHERE MATCH clauses, I used to perform exact string MATCH as follows: WHERE MATCH(some_column) AGAINST('\"$some_term\"') It worked perfectly. Now I have switched to using PDO bindValue as follows: WHERE MATCH(some_column) AGAINST(:some_term) When binding a string with multiple words such as "orange bicycle", I get results for "orange" and "bicycle" whereas what I want is an exact match for "orange bicycle". How do I force an exact

Trying to connect to access database with PDO

会有一股神秘感。 提交于 2020-07-03 08:00:31
问题 I am trying to connect to access database that I have on the c drive. I uncommented access extension pdo in INI file. I ran the driver test and it shows odbc driver is installed. I am using wamp with apache server but I keep getting this error SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified <?php try { $conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\staffing.mdb;Uid=Admin"); } catch

PHP PDO - There is no active transaction

为君一笑 提交于 2020-07-03 06:45:18
问题 I am having problem with transactions in php script. I would like to make multiply queries and be able to recall them all, if at least one of them fails. Below you can find a simple example of the script I am using: $tags_input = array(6,4,5); $conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSW, array( PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")); $conn-

How do I bind an INTERVAL param with PDO?

浪子不回头ぞ 提交于 2020-07-03 03:42:27
问题 I have a query that I am trying to set up entirely from bound parameters, but right now I'm having to resort to appending a string from a GET parameter (I don't really want to do that). Here's my solution right now: $interval = !empty($_REQUEST['interval']) ? $_REQUEST['interval'] : '28 DAY'; $interval = str_replace('_', ' ', $interval); $data = array( ':msisdn' => $_REQUEST['msisdn'] ); $sql = <<<SQL SELECT COUNT(*) AS `countup` FROM ( SELECT utc.`id_user` FROM `user_to_cli` utc WHERE 1=1

How do I bind an INTERVAL param with PDO?

谁都会走 提交于 2020-07-03 03:42:06
问题 I have a query that I am trying to set up entirely from bound parameters, but right now I'm having to resort to appending a string from a GET parameter (I don't really want to do that). Here's my solution right now: $interval = !empty($_REQUEST['interval']) ? $_REQUEST['interval'] : '28 DAY'; $interval = str_replace('_', ' ', $interval); $data = array( ':msisdn' => $_REQUEST['msisdn'] ); $sql = <<<SQL SELECT COUNT(*) AS `countup` FROM ( SELECT utc.`id_user` FROM `user_to_cli` utc WHERE 1=1

Which one is safer to use in OOP?

我的梦境 提交于 2020-06-28 04:56:24
问题 I learned these 2 methods in PDO with OOP when I study it and I would like to ask which is safer to use? binding everything we used or just using ? and execute it. 1: public function query($query) { $this->stmt = $this->dbh->prepare($query); } public function bind($param, $value, $type = null) { if (is_null($type)) { switch(true){ case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; break;