pdo

How to fetch a row with PDO

二次信任 提交于 2020-01-09 09:37:49
问题 I'm trying not to get frustrated, but I've recently learned that mysql_* is deprecated in PHP. I've decided that I would learn how to use PDO. I've just been looking at it this afternoon, and connecting to the database using it was easy, but then I wanted to fetch a row with it and save the row as an array indexed by the column names (the same way as the function mysql_fetch_array did). I can't figure it out to save my life. I'll post my code to clarify, and I'm sure it is something simple

How to fetch a row with PDO

大憨熊 提交于 2020-01-09 09:37:32
问题 I'm trying not to get frustrated, but I've recently learned that mysql_* is deprecated in PHP. I've decided that I would learn how to use PDO. I've just been looking at it this afternoon, and connecting to the database using it was easy, but then I wanted to fetch a row with it and save the row as an array indexed by the column names (the same way as the function mysql_fetch_array did). I can't figure it out to save my life. I'll post my code to clarify, and I'm sure it is something simple

PDO::exec() or PDO::query()?

故事扮演 提交于 2020-01-09 06:19:50
问题 I used to have this as one of the options (4th param) passed to PDO constructor: $aOptions[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES utf8"; But just found that it does not work on certain php versions on Windows (does not work in 5.3) due to some bug. Now I need to run SET NAMES utf8 using either $pdo->exec("SET NAMES utf8"); or $pdo->query("SET NAMES utf8"); right after the instantiating the pdo object. So, which one should I use - exec() or query()? 回答1: When using PDO::EXEC the result

Oracle 11g XE greek character set not displaying

孤人 提交于 2020-01-09 05:40:18
问题 I used an Oracle 11g EE to create my database and access it through PHP using PDO, and all was working good. But i had to change to 11g XE because i wanted to use it freely inside my company. Now, greek characters won't display on PHP (i get �������� instead). I tried to change the character set with : shutdown immediate; startup mount; alter system enable restricted session; alter system set job_queue_processes=0; alter database open; alter database character set internal_use EL8MSWIN1253;

What is the best way to bind decimal / double / float values with PDO in PHP?

前提是你 提交于 2020-01-09 05:20:32
问题 It appears the class constants only cover PDO::PARAM_BOOL , PDO::PARAM_INT and PDO::PARAM_STR for binding. Do you just bind decimal / float / double values as strings or is there a better way to treat them? MySQLi allows the 'd' type for double, it's surprising that PDO doesn't have an equivalent when it seems better in so many other ways. 回答1: AFAIK PDO::PARAM_STR is the way to go. 回答2: You have to use PDO::PARAM_STR, but for SQLite and other engines this can have unpredictable behaviors.

Setting a connect timeout with PDO

余生长醉 提交于 2020-01-09 03:43:46
问题 I'm using PDO to get data off a MySQL server. What I noticed is this: if the MySQL server is unavailable, it takes really (relatively) long for this code to return an exception: try { $handle = new PDO($db_type . ':host='.$db_host.';dbname='.$db_name,$db_user,$db_pass); // Tried using PDO::setAttribute and PDO::ATTR_TIMEOUT here } catch(PDOException $e) { echo $e->getMessage; } In case of MySQL it takes just over 2 minutes for the exception to occur (SQLSTATE[HY000] [2003] Can't connect to

How do I use pdo's prepared statement for order by and limit clauses?

时光总嘲笑我的痴心妄想 提交于 2020-01-08 16:35:34
问题 I want to use a prepared statement in which the passed-in parameters are for the ORDER BY and LIMIT clauses, like so: $sql = 'SELECT * FROM table ORDER BY :sort :dir LIMIT :start, :results'; $stmt = $dbh->prepare($sql); $stmt->execute(array( 'sort' => $_GET['sort'], 'dir' => $_GET['dir'], 'start' => $_GET['start'], 'results' => $_GET['results'], ) ); But $stmt->fetchAll(PDO::FETCH_ASSOC); returns nothing. Can someone point out what's the wrong thing I am doing? Can it be done? If not,what

How do I use pdo's prepared statement for order by and limit clauses?

做~自己de王妃 提交于 2020-01-08 16:35:04
问题 I want to use a prepared statement in which the passed-in parameters are for the ORDER BY and LIMIT clauses, like so: $sql = 'SELECT * FROM table ORDER BY :sort :dir LIMIT :start, :results'; $stmt = $dbh->prepare($sql); $stmt->execute(array( 'sort' => $_GET['sort'], 'dir' => $_GET['dir'], 'start' => $_GET['start'], 'results' => $_GET['results'], ) ); But $stmt->fetchAll(PDO::FETCH_ASSOC); returns nothing. Can someone point out what's the wrong thing I am doing? Can it be done? If not,what

PDO::fetchAll vs. PDO::fetch in a loop

﹥>﹥吖頭↗ 提交于 2020-01-08 11:17:29
问题 Just a quick question. Is there any performance difference between using PDO::fetchAll() and PDO::fetch() in a loop (for large result sets)? I'm fetching into objects of a user-defined class, if that makes any difference. My initial uneducated assumption was that fetchAll might be faster because PDO can perform multiple operations in one statement while mysql_query can only execute one. However I have little knowledge of PDO's inner workings and the documentation doesn't say anything about

php中PDO处理mysql 基本操作

谁说我不能喝 提交于 2020-01-07 18:44:22
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一 :php连接mysql 代码: ‍‍ <?php $servername = "127.0.0.1"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=mysql", $username, $password); echo "Connected successfully"; } catch(PDOException $e) { echo $e->getMessage(); } ?> ‍‍ 二 :insert,update,delete的语法类似,仅仅以insert为例,将以下代码扔到try块中就ok. 代码: $sql="insert into test values(1,'a')"; $num=$conn->exec($sql); echo "影响行数 $num"; 三: select的语法略微复杂. 代码: $sql="select * from test"; $res=$conn->query($sql); foreach($res as $r){ echo $r["id"] . "<br>" //$r是一个array,我们仅仅输出了id列