UPDATE mysqli_query()的正确语法/方法



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"被每个查询调用。任何解释我做错了什么将是非常感激!

SET description=$line[5]应加引号

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

相关内容

  • 没有找到相关文章

最新更新