[英]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.