简体   繁体   English

PHP PDO MySQL没有异常或错误

[英]PHP PDO MySQL No Exception or Error

I've got the following script that uses PDO. 我有以下使用PDO的脚本。

    $db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);

    $sql    = "BEGIN;\n".
              "INSERT INTO exp_ws_gk_text (WGT_PRG_CODE, WGT_TEXT)\n".
              "    VALUES('".$_POST['prg_code']."', ".$db->quote($_POST['text']).");\n" .
              "SELECT @Lid:=LAST_INSERT_ID();\n" .
              "INSERT INTO exp_ws_gk_state (WGS_STATE, WGS_TYPE, WGS_WGT_RID)\n".
              "    VALUES('".$_POST['state']."', ".$_POST['type'].", @Lid);\n".
              "SELECT a.wgt_prg_code prg_code, b.wgs_state state, b.wgs_type type, a.wgt_rid id, a.wgt_text text\n".
              "    FROM exp_ws_gk_text a\n".
              "    JOIN exp_ws_gk_state b ON b.wgs_wgt_rid = a.wgt_rid\n".
              "    WHERE b.wgs_wgt_rid = @Lid ORDER BY a.wgt_prg_code;\n".
              "COMMIT;\n";

$fp = fopen("/var/tmp/save_gatekeeper.txt", "a");
fwrite($fp, "SQL:". $sql."\n");

    try {
        $res = $db->prepare($sql);
        $res->execute();
        $error = $db->errorInfo();

fwrite($fp, "ErrorInfo:".print_r($error, true)."\n\n");
fwrite($fp, "Row:      ".print_r($res->fetch(PDO::FETCH_ASSOC), true)."\n\n");
fwrite($fp, "Row2:     ".print_r($res->fetch(PDO::FETCH_LAZY),true)."\n\n");

    }
    catch (PDOException $e) {

fwrite($fp, "ExceptionInfo:".print_r($e, true)."\n\n");

    }    

fclose($fp);

    echo json_encode($res->fetch(PDO::FETCH_ASSOC););

This is what the log produced: 这是产生的日志:

SQL:BEGIN;
INSERT INTO exp_ws_gk_text (WGT_PRG_CODE, WGT_TEXT)
    VALUES('EDS', '[h3]IMPORTANT INFORMATION[/h3][p]This is a test gatekeeper for [b]Eddie\'s[/b] Classroom Course[/p]');
SELECT @Lid:=LAST_INSERT_ID();
INSERT INTO exp_ws_gk_state (WGS_STATE, WGS_TYPE, WGS_WGT_RID)
    VALUES('AL', 1, @Lid);
SELECT a.wgt_prg_code prg_code, b.wgs_state state, b.wgs_type type, a.wgt_rid id, a.wgt_text text
    FROM exp_ws_gk_text a
    JOIN exp_ws_gk_state b ON b.wgs_wgt_rid = a.wgt_rid
    WHERE b.wgs_wgt_rid = @Lid ORDER BY a.wgt_prg_code;
COMMIT;

ErrorInfo:Array
(
    [0] => 00000
)


Row:      

Row2:     

I'm not sure why it isn't reporting any kind of error or exception. 我不确定为什么它没有报告任何类型的错误或异常。 This is the first time I'm using PDO. 这是我第一次使用PDO。 I converted from using the older mysql calls. 我从使用较旧的mysql调用转换而来。

Can someone help out here? 有人可以帮忙吗?

I'm expecting the results from the last select to be sent back, all I get is "false"; 我希望最后一次选择的结果能被发回,我得到的只是“假”。

Since you're using a prepared statement with multiple queries, you need to use emulated prepared statements. 由于您要使用带有多个查询的预备语句,因此需要使用模拟的预备语句。 Refer to PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND) 请参阅PDO对多个查询的支持(PDO_MYSQL,PDO_MYSQLND)

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);

Otherwise, I believe the only thing you are executing is BEGIN; 否则,我相信您正在执行的唯一操作就是BEGIN;


In my personal opinion, I think this is bad practice to put multiple queries into one PDO statement. 我个人认为,将多个查询放入一个PDO语句中是一种不好的做法。 You should consider using a transaction with PDO and multiple statements which allows you to more easily figure which query is causing issues and rollback if an error occurs. 您应该考虑将事务与PDO和多个语句一起使用,这使您可以更轻松地确定哪个查询引起了问题并在发生错误时回滚。


Secondly, you are using prepared statements but are still not protecting yourself from SQL injections. 其次,您正在使用准备好的语句,但仍无法保护自己免受SQL注入的侵害。 Don't use $_POST in the prepare(). 不要在prepare()中使用$ _POST。 You are supposed to bind those after you prepare the statement. 准备语句后,应将其绑定。

Cheers, we solved this in chat. 干杯,我们在聊天中解决了这个问题。

You're mixing multiple types of statements. 您正在混合多种类型的语句。 You need to separate out your queries into two separate queries; 您需要将查询分为两个单独的查询; one with the INSERT with $res->query , and then validate the response, and then another with the SELECT using $res->fetch() or $res->fetchAll() 一个使用INSERT$res->query ,然后验证响应,然后另一个使用SELECT使用$res->fetch()$res->fetchAll()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM