Php 从MySQL数据库中删除字符
对不起-我是一个完全的新手,所以非常感谢任何帮助 我在mysql数据库的一列(texty3)中有大约30000个条目,需要删除某些字符/单词: 具体来说, 少于3个字符的任何单词。 任何没有大写字母的单词。 任何非字母数字的东西 我想知道是否有一个mysql命令用于此 我让某人编写了一个脚本,用于将来通过php上传,如下所示Php 从MySQL数据库中删除字符,php,mysql,Php,Mysql,对不起-我是一个完全的新手,所以非常感谢任何帮助 我在mysql数据库的一列(texty3)中有大约30000个条目,需要删除某些字符/单词: 具体来说, 少于3个字符的任何单词。 任何没有大写字母的单词。 任何非字母数字的东西 我想知道是否有一个mysql命令用于此 我让某人编写了一个脚本,用于将来通过php上传,如下所示 function getmtext($str) { $text = ''; $words = str_word_count($str, 1); fo
function getmtext($str) {
$text = '';
$words = str_word_count($str, 1);
foreach ($words as $word) {
if ($word[0] >= 'A' && $word[0] <= 'Z')
if (strlen($word)>3)
$text .= $word.' ';
}
return $text;
}
函数getmtext($str){
$text='';
$words=str\u word\u计数($str,1);
foreach($words作为$word){
如果($word[0]>='A'&&$word[0]3)
$text.=$word.';
}
返回$text;
}
但我不知道如何编辑已经存在的条目
感谢您的帮助 您可以使用相同的脚本来更新SQL中的条目,但是(我知道)没有办法按照您的要求使用纯SQL 也就是说,您可以从数据库中检索数据(使用mysql函数),修改信息并将其返回到数据库 出于参数考虑,如果您在本地拥有名为test的数据库,并且没有以root用户身份登录密码,那么您可以使用以下方法:
function getmtext($str) {
$text = '';
$words = str_word_count($str, 1);
foreach ($words as $word) {
if ($word[0] >= 'A' && $word[0] <= 'Z')
if (strlen($word)>3)
$text .= $word.' ';
}
return $text;
}
//set the database parameters
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbName = "test";
//create the database connection
$dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());
//sql to get all texty3 rows from table
$sql = "select id, texty3 from testtable";
$query = mysql_query($sql); //run the sql
while($result = mysql_fetch_assoc($query)) { //loop through each row
$rowId = $result['id'];
$text = $result['texty3'];
$cleansedText = getmtext($text);
mysql_query("update testtable set texty3 = '$cleansedText' where id = '$rowId';");
}
函数getmtext($str){
$text='';
$words=str\u word\u计数($str,1);
foreach($words作为$word){
如果($word[0]>='A'&&$word[0]3)
$text.=$word.';
}
返回$text;
}
//设置数据库参数
$dbHost=“localhost”;
$dbUser=“root”;
$dbPass=“”;
$dbName=“test”;
//创建数据库连接
$dbConn=mysql\u connect($dbHost、$dbUser、$dbPass)或die('mysql connect failed')。mysql\u error();
mysql_select_db($dbName)或die('cannotselect database')。mysql_error();
//sql从表中获取所有texty3行
$sql=“从testtable中选择id,texty3”;
$query=mysql\u查询($sql)//运行sql
而($result=mysql\u fetch\u assoc($query)){//循环遍历每一行
$rowId=$result['id'];
$text=$result['texty3'];
$cleansedText=getmtext($text);
mysql_查询(“更新测试表集texty3='$cleanedText',其中id='$rowId';”;
}
这将遍历数据库并使用该函数删除与函数中定义的参数不匹配的所有单词。(很抱歉,我现在无法检查此代码,但它应该是正确的或至少接近正确的)我认为您必须编写一个PHP脚本来批处理数据库中的所有条目,MySQL没有内置函数来完成您所需的操作。