📜  php mysql foreach 是坏的 pardics 吗? - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:57.690000             🧑  作者: Mango

代码示例1
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Errore:" .$e->getMessage();
    die();
}  

# Data
$file = file_get_contents($filejson);
if (!function_exists('json_decode')) {
    die('Il server non ha tale funzione');
}   
$result = json_decode($file, true);

# Prepare and execute statement
try {
    $sql = 
        "INSERT INTO ibl_Anag (TicketId,otherId,ClientId,Name,LName,MobPhone,Phone,address,mail)
        VALUES (?,?,?,?,?,?,?,?,?)";
    $stmt = $connessione->prepare($sql);
    foreach ($result["Contacts"] as $row) {
        $ticketId = $row["TicketId"];
        $otherId  = $row["otherId"];
        $clientId = $row["ClientId"];
        $name     = $row["Name"];
        $lname    = $row["LName"];
        $mobPhone = $row["Phone"];
        $phone    = $row["Phone"];
        $address  = $row["ADDRESS"];
        $mail     = $row["mail"];
        $stmt->BindParam(1, $ticketId);
        $stmt->BindParam(2, $otherId); 
        $stmt->BindParam(3, $clientId);
        $stmt->BindParam(4, $name);    
        $stmt->BindParam(5, $lname);   
        $stmt->BindParam(6, $mobPhone);
        $stmt->BindParam(7, $phone);   
        $stmt->BindParam(8, $address);
        $stmt->BindParam(9, $mail);    
        if (!$stmt->execute()) {
            echo "la insert non ha funzionato";
        }
    }                       
} catch(PDOException $e) {
    echo "Errore:" .$e->getMessage();
    die();
}  
?>