0

好的,我在 php 脚本中对 mysql 中的表进行了更新:

   mysqli_query($con,"UPDATE ted SET description=$line[5]
    WHERE speaker='$speaker' AND event='$event'");

它有什么问题吗?它在我的脚本中正常运行(没有错误)但是即使没有更新:它被多次调用(并且具有与 WHERE 条件匹配的行)这是整个脚本

<?php
$contents = file_get_contents('ted_csv.txt');
$con=mysqli_connect("localhost","root","admin","Media2net");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 $result = mysqli_query($con,"SELECT * FROM ted");

 $i=0;
 while($row = mysqli_fetch_array($result))
 {
    $speaker =$row['speaker'];
    $event =$row['event'];
    $pattern2 = preg_quote($speaker, '/');
    $pattern1 = preg_quote($event, '/');
    $pattern1 = "/^.*$pattern1.*\$/m";

    $pattern2 = "/^.*$pattern2.*\$/m";

    if(preg_match_all($pattern1, $contents, $matches)){ 
            $submatch =  implode("\n", $matches[0]);

       if (preg_match_all($pattern2, $submatch, $better)){ 
          echo "got match" .  $i . "\n";
          $line =str_getcsv( $better[0][0] );

          $a_bool = mysqli_query($con,"UPDATE ted SET description=$line[5]
           WHERE speaker='$speaker' AND event='$event'");
          if ($a_bool){
             echo "got match" .  $i . "\n";
          }else{
             echo "query unsuccesful for match" .  $i . "\n";
          }

       }
    }

    $i++;
}//end of while loop results
mysqli_close($con);
?>

如您所见,匹配应该可用,因为$speaker and $event从表 ted 中提取了相同的字符串

很明显,我的语法有问题,因为每个查询$a_bool都是错误的,导致 每个查询都echo "query unsuccessful for match" . $i . "\n"被调用。任何我做错了什么的解释将不胜感激!

4

1 回答 1

2

SET description=$line[5] 应该在引号中

喜欢

mysqli_query($con,"UPDATE ted SET description='{$line[5]}'
WHERE speaker='$speaker' AND event='$event'");
于 2013-06-19T08:30:50.693 回答