pdo

thinkphp获取mysql错误

人走茶凉 提交于 2020-03-01 21:42:55
最近用thinkphp时发现无法捕获mysql的错误信息,例如向表中插入一个主键 重复的数据时,程序便卡在那条语句上,就像下面这样 :( SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' 错误位置 FILE: D:\*****\ThinkPHP\Library\Think\Db\Driver.class.php  LINE: 226 TRACE #0 D:\*****\ThinkPHP\Library\Think\Db\Driver.class.php(226): PDOStatement->execute() 在代码中 使用try catch 无效,最后修改 Driver.class.php文件 中169行和226行的 $result = $this->PDOStatement->execute(); 修改为 try { //lighthouse $result = $this -> PDOStatement ->execute() ; } catch (\PDOException $e ) { echo 'queryStr=' . $this -> queryStr . '<br/>' ; print "Error: " . $e -

Trying PDO wrapper and getting result as 'NULL' when there is a record present in the database [duplicate]

社会主义新天地 提交于 2020-02-29 10:03:48
问题 This question already has an answer here : php function returns null instead of string (1 answer) Closed 29 days ago . I just tried a PDO wrapper from https://phpdelusions.net/pdo/pdo_wrapper. First the PDO Wrapper class MyPDO { protected static $instance; protected $pdo; public function __construct() { $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_EMULATE_PREPARES => FALSE, ); $dsn = 'mysql:host='.DB_HOST.';dbname='.DB

Trying PDO wrapper and getting result as 'NULL' when there is a record present in the database [duplicate]

 ̄綄美尐妖づ 提交于 2020-02-29 10:00:41
问题 This question already has an answer here : php function returns null instead of string (1 answer) Closed 29 days ago . I just tried a PDO wrapper from https://phpdelusions.net/pdo/pdo_wrapper. First the PDO Wrapper class MyPDO { protected static $instance; protected $pdo; public function __construct() { $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_EMULATE_PREPARES => FALSE, ); $dsn = 'mysql:host='.DB_HOST.';dbname='.DB

Trying PDO wrapper and getting result as 'NULL' when there is a record present in the database [duplicate]

点点圈 提交于 2020-02-29 10:00:11
问题 This question already has an answer here : php function returns null instead of string (1 answer) Closed 29 days ago . I just tried a PDO wrapper from https://phpdelusions.net/pdo/pdo_wrapper. First the PDO Wrapper class MyPDO { protected static $instance; protected $pdo; public function __construct() { $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_EMULATE_PREPARES => FALSE, ); $dsn = 'mysql:host='.DB_HOST.';dbname='.DB

php -- PDO事务处理

泪湿孤枕 提交于 2020-02-29 09:33:28
事务处理流程 开启事务 PDO::beginTransaction 事务操作 所有的实务操作就是增删改 事务提交 PDO::commit:成功后提交数据 PDO::rollback:失败后回滚数据 <?php //PDO事务处理 //连接认证 $pdo = new PDO('mysql:host=localhost;dbname=project','root','root'); //1. 开启事务 $res = $pdo->beginTransaction(); //var_dump($res); //2. 事务处理(多条SQL语句执行) $sql = "update pro_student set s_age = 28 where s_id = 20"; $lines = $pdo->exec($sql); //var_dump($lines); $sql = "select * from pro_student where s_id = 20"; $stmt = $pdo->query($sql); //var_dump($stmt->fetch(PDO::FETCH_ASSOC)); //3. 提交事务 if($lines){ //更新成功 $pdo->commit(); }else{ //更新失败 echo '失败'; $pdo->rollback(); } 注意

json_encode returning the next rows values - PHP PDO SQL HighCharts

☆樱花仙子☆ 提交于 2020-02-29 06:57:45
问题 When trying to use json_encode from arrays in foreach() the data moves in to the next values, for example: I have this table which is coming from the same query + php: when using json_encode to try and fit this tables data in to highcharts, my outcome is like this: [{"name":"User1","data":[0,2,0,0]},{"name":"User2","data":[0,2,0,0,2,0,2,4]}] So it's moving User1's data in to User2's My desired outcome would be: [{"name":"User1","data":[0,2,0,0]},{"name":"User2","data":[2,0,2,4]}] This is my

json_encode returning the next rows values - PHP PDO SQL HighCharts

邮差的信 提交于 2020-02-29 06:57:05
问题 When trying to use json_encode from arrays in foreach() the data moves in to the next values, for example: I have this table which is coming from the same query + php: when using json_encode to try and fit this tables data in to highcharts, my outcome is like this: [{"name":"User1","data":[0,2,0,0]},{"name":"User2","data":[0,2,0,0,2,0,2,4]}] So it's moving User1's data in to User2's My desired outcome would be: [{"name":"User1","data":[0,2,0,0]},{"name":"User2","data":[2,0,2,4]}] This is my

PHP- Trying to convert Mysqli to PDO

拥有回忆 提交于 2020-02-29 06:52:10
问题 I am trying to convert the Mysqli code to use PDO Mysqli code looks like the following (which works great) $rs = "SELECT * FROM team"; $result = mysqli_query($con,$rs); $data = mysqli_num_rows($result); $responses = array(); if($data != 0) { while($results = mysqli_fetch_assoc($result)) { echo "<tr><td>".$results['code'] ."</td>"; echo "<td>".$results['username'] ."</td>"; } My PDO code I tried $stmt = $con->prepare("select * from team"); $stmt->execute(); if($stmt->rowCount() > 0) $result =

PHP- Trying to convert Mysqli to PDO

旧巷老猫 提交于 2020-02-29 06:52:06
问题 I am trying to convert the Mysqli code to use PDO Mysqli code looks like the following (which works great) $rs = "SELECT * FROM team"; $result = mysqli_query($con,$rs); $data = mysqli_num_rows($result); $responses = array(); if($data != 0) { while($results = mysqli_fetch_assoc($result)) { echo "<tr><td>".$results['code'] ."</td>"; echo "<td>".$results['username'] ."</td>"; } My PDO code I tried $stmt = $con->prepare("select * from team"); $stmt->execute(); if($stmt->rowCount() > 0) $result =

PHP PDO prepare()、execute()和bindParam()方法详解

試著忘記壹切 提交于 2020-02-28 16:42:46
每次将查询发送给MySQL服务器时,都必须解析该查询的语法,确保结构正确并能够执行。这是这个过程中必要的步骤,但也确实带来了一些开销。做一次是必要的,但如果反复地执行相同的查询,批量插入多行并只改变列值时会怎么样呢?预处理语句会在服务器上缓存查询的语法和执行过程,而只在服务器和客户端之间传输有变化的列值,以此来消除这些额外的开销。 PDO为支持此特性的数据库提供了预处理语句功能。因为MySQL支持这个特性,所以可以在适当时使用预处理语句。 预处理语句是使用两个方法实现的:prepare()方法负责准备要执行的查询,execute()方法使用一组给定的列参数反复地执行查询。这些参数可以显式地作为数组传递给execute()方法,也可以使用通过bindParam()方法指定的绑定参数提供给execute()方法。 来源: oschina 链接: https://my.oschina.net/u/1022390/blog/717874