簡體   English   中英

使用當前行值的值進行表更新

[英]using value of current row value for table update

在我的表中,我有大約120個網址(主鍵)。 現在我添加了一個hash ,它采用這樣的哈希值

$hash = md5($url);  

我想刪除url作為主鍵(我做了)並將hash設置為主鍵。

目前,當我嘗試將哈希作為主鍵時,請說dublicate entry錯誤。 因為哈希的所有120條目都是空的。

所以我想更新我的表,以便hash應該設置為hash = md5(url)。

我的嘗試:

<?php
    $con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');                
    if (mysqli_connect_errno())    
    {    
        echo "Failed to connect to MySQL: " . mysqli_connect_error();    
        return;    
    }    
    $result = mysqli_query($con,"SELECT url from frrole_cateogry_article");    
    while ($row = @mysqli_fetch_array($result))    
    {    
        $url = $row['url'];
        $hash = md5($url);
        $update = "UPDATE table frrole_cateogry_article set hash='".$hash."' where url = '".$url."'";           
        if (!mysqli_query($con,$update))    
        {    
            //die('Error: ' . mysqli_error($con)); 
            echo "error";
        }       
    }           
?>

die('Error: ' . mysqli_error($con));消息die('Error: ' . mysqli_error($con));

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table frrole_popular_article set hash='3402a8ee11df088cd4e4a270dacbcc98' where u' at line 1

但它給出了錯誤回顯error消息。

怎么了?

UPDATE frrole_cateogry_article SET hash=md5(url)

而已。 沒有循環,也沒有選擇。 這將在每一行上將hash設置為同一行中其url md5

MySQL也支持MD5

你可以這樣做:

$update = "UPDATE frrole_cateogry_article set hash=md5( url_field )";

此語句將所有記錄URL更新為md5哈希。 不需要while循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM