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

时间:2013-06-19 08:25:54

标签: php mysql sql-update

好的,我在php脚本中的mysql中有一个UPDATE表的语法:

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

它有什么问题吗?它在我的scriipt中正常运行(没有错误)但是没有更新,即使:它被多次调用(并且具有与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是从表格中提取的

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

1 个答案:

答案 0 :(得分:2)

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

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