例如:文件 2.txt 内容如下
123 234 abc
php脚本
file.php
<?php $con = mysql_connect("localhost","root","111111"); if (!$con) { die('Could not connect:' .mysql_error()); } mysql_select_db("zzadmin",$con); $file=file('2.txt'); $arrlength=count($file); for ($x=0;$x<$arrlength;$x++) { $b=$file[$x]; $sql = "INSERT INTO csdn_db (username,passwd,email) SELECT username,passwd,email FROM csdn where passwd like '$b%'"; var_dump($sql); echo "<p>"; ?>
如图1
查阅资料发现可以通过PHP trim()函数解决此问题
方法如下:
trim() 去除一个字符串两端空格,
rtrim() 是去除一个字符串右部空格,
ltrim() 是去除一个字符串左部空格。
-------------------------------------
更改脚本:
<?php $con = mysql_connect("localhost","root","111111"); if (!$con) { die('Could not connect:' .mysql_error()); } mysql_select_db("zzadmin",$con); $file=file('2.txt'); $arrlength=count($file); for ($x=0;$x<$arrlength;$x++) { $c=$file[$x]; $b=rtrim($c); //添加函数 $sql = "INSERT INTO csdn_db (username,passwd,email) SELECT username,passwd,email FROM csdn where passwd like '$b%'"; var_dump($sql); echo "<p>"; >
如图2
OK 完美解决