没有使用键列的WHERE的SQL

时间:2013-09-09 18:40:53

标签: php sql magento

我的脚本正在退出不应该有问题的SQL调用。其中一个无法更新的查询是:

UPDATE dist_comp.private_schools SET mail_address = '1351 Royalty Dr', city = 'Alabaster', state = 'AL',zip_code = 35007,phone = '2056633973' WHERE school_name = 'Kingwood Christian School' AND city = 'Alabaster'

当我在MySQL工作台中运行相同的查询时,我得到了

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.

这就是我的脚本退出的原因吗?

<?php
require_once('connect.php');

function schoolInfo($school_name,$city){
    $data = array();
    $counter = 0;  
    $handle = fopen("k12privateschools_loc_0813.csv","r") or exit('doesnt work');
    fgetcsv($handle) or exit('fgetcsv issue');

    while( ($line = fgetcsv($handle)) !== false) {
        $data[] = $line;

        ///echo ("Does schoolname and city match: " . addslashes($data[$counter][2]). ":" . $school_name . " ; " . addslashes($data[$counter][4]). ":" . $city . "\n");

        if (addslashes($data[$counter][2])==$school_name && addslashes($data[$counter][4])==$city){
            //echo ('match');       
            if($data[$counter][13] != ""){
                $mail_address = $data[$counter][12];
                $city= $data[$counter][13];
                $state= $data[$counter][14];
                $zip_code= $data[$counter][15];
                $zip_4= $data[$counter][16];
            }else{
                $mail_address = $data[$counter][3];
                $city= $data[$counter][4];
                $state= $data[$counter][5];
                $zip_code= $data[$counter][6];
                $zip_4= $data[$counter][7];
            }
            $phone= $data[$counter][8];

            $query= "UPDATE dist_comp.private_schools SET  
                    mail_address = '".$mail_address."', 
                    city = '".$city."', 
                    state = '".$state."',"; 
            if($zip_code != ""){
                    $query.="zip_code = ".$zip_code.",";
            }
            if($zip_4 != ""){
                $query.="zip_4 = ".$zip_4.",";
            }
                    $query.= "phone = '".$phone."' 
                    WHERE school_name = '".$school_name."' AND city = '" .$city . "'";

            mysqli_query($con,$query);
            if(mysqli_affected_rows($con)==0){
                exit($query . "\n ");
            }
            //echo $query;
        }//end if counter \
        else{
            //echo("no match");
            }
        $counter++;
    }//end read lines from file
}
    echo "starting import \n";

        //Query for all school names
        $sql2 = mysqli_query($con,"SELECT school_name,city FROM dist_comp.private_schools") or exit('query issue second');
            while($row = mysqli_fetch_array($sql2)){ //this line is making it take a really long time
                $school_name= addslashes($row['school_name']);
                $city = addslashes($row['city']);
                schoolInfo($school_name,$city);
            }//end while fetch array

    //}
  echo "Import finished";
?>

2 个答案:

答案 0 :(得分:0)

尝试在查询前使用此行停用safe update

 mysqli_query($con,"SET sql_safe_updates=0");

或使用:

$query="SET sql_safe_updates=0";

$query.= "UPDATE dist_comp.private_schools SET  
                mail_address = '".$mail_address."', 
                city = '".$city."', 
                state = '".$state."';"; 


mysqli_multi_query($con,$query);

或在MySQL WorkBench中:

  • 编辑 - &gt;偏好 - &gt; SQL查询
  • 取消选中禁止UPDATE和DELETE语句而不使用WHERE子句 (安全更新)
  • 查询 - &gt;重新连接到服务器

答案 1 :(得分:0)

$query = 'SET SQL_SAFE_UPDATES=0;';

$query .= 'custom query here;';

$query .= 'SET SQL_SAFE_UPDATES=1;';