Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php的神秘行为_Php_Mysql_Pdo - Fatal编程技术网

php的神秘行为

php的神秘行为,php,mysql,pdo,Php,Mysql,Pdo,下面的代码按预期工作。它在“关键字”表中添加了3个条目 <?php include "config.php"; try{ // $conn = new PDO(DBINFO,USER,PASS); // $sql = "INSERT INTO projects (title,duration, startyear, description, tags,email) VALUES (:title,:duration, :startyear, :description, :ta

下面的代码按预期工作。它在“关键字”表中添加了3个条目

<?php
include "config.php";
try{
    // $conn = new PDO(DBINFO,USER,PASS);
    // $sql = "INSERT INTO projects (title,duration, startyear, description, tags,email) VALUES (:title,:duration, :startyear, :description, :tags,:email)";
    // $stmt = $conn->prepare($sql);
    // $stmt->bindParam(':title', $_POST['title'],PDO::PARAM_STR);
    // $stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
    // $stmt->bindParam(':duration', $_POST['duration'], PDO::PARAM_STR);
    // $stmt->bindParam(':startyear', $_POST['startyear'], PDO::PARAM_STR);
    // $stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
    // $stmt->bindParam(':tags', $_POST['tags'], PDO::PARAM_STR);
    // $stmt->execute();

    for($i=0; $i<3; $i++){
        $conn2 = new PDO(DBINFO,USER,PASS);
        $sql2 = "INSERT INTO keywords (keyword,confidence) VALUES (:keyword,:confidence)";
        $stmt2 = $conn2->prepare($sql2);
        $a = 'asdfds';
        $stmt2->bindParam(':keyword', $a,PDO::PARAM_STR);
        $stmt2->bindParam(':confidence', $a, PDO::PARAM_STR);
        $stmt2->execute();
    }
}
catch(PDOException $pe){
    die("Could not connect to the database :".$pe->getMessage());
}
?>

但是,当我运行下面的代码时(我取消了第一部分的注释),条目会被添加到“关键字”表中6次

<?php
include "config.php";
try{
    $conn = new PDO(DBINFO,USER,PASS);
    $sql = "INSERT INTO projects (title,duration, startyear, description, tags,email) VALUES (:title,:duration, :startyear, :description, :tags,:email)";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':title', $_POST['title'],PDO::PARAM_STR);
    $stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
    $stmt->bindParam(':duration', $_POST['duration'], PDO::PARAM_STR);
    $stmt->bindParam(':startyear', $_POST['startyear'], PDO::PARAM_STR);
    $stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
    $stmt->bindParam(':tags', $_POST['tags'], PDO::PARAM_STR);
    $stmt->execute();

    for($i=0; $i<3; $i++){
        $conn2 = new PDO(DBINFO,USER,PASS);
        $sql2 = "INSERT INTO keywords (keyword,confidence) VALUES (:keyword,:confidence)";
        $stmt2 = $conn2->prepare($sql2);
        $a = 'asdfds';
        $stmt2->bindParam(':keyword', $a,PDO::PARAM_STR);
        $stmt2->bindParam(':confidence', $a, PDO::PARAM_STR);
        $stmt2->execute();
    }
}
catch(PDOException $pe){
    die("Could not connect to the database :".$pe->getMessage());
}
?>


我不明白。有什么帮助吗?

首先为什么要创建4个不同的连接到同一个服务器和架构

循环创建连接,并在覆盖对语句和连接的引用时自动关闭它们

但是循环之前的原始连接将保持打开状态,并被语句重用。如果在循环之前创建第三个连接而不关闭它,那么最终将有9个条目

因此,如果不再需要连接对象(包括关联语句),请删除对连接对象的引用


或者更好地重用连接,而不是为每个语句创建新连接

请使用bindValue而不是bindParam进行测试,并保留第三个/类型参数。