下面是PHP代码我使用PHP“str_replace”在某些情况下无法正常工作?
$file_handle = fopen("products.csv", "r");
$fname = "products.csv";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$server = "**\******,1433";
$connectionInfo = array("Database"=>"******", "UID"=>"***", "PWD"=>"*******");
$conn = sqlsrv_connect($server, $connectionInfo);
if($conn === false) {
die(print_r(sqlsrv_errors(), true));
}
while (!feof($file_handle)) {
$line_of_text = fgetcsv($file_handle, 1024);
$itemco = $line_of_text[0];
$sql = "SELECT quantity FROM Item WHERE itemlookupcode = '$itemco' ";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$icc = $row['quantity'];
$content = str_replace("$line_of_text[1]", "$icc", "$content");
}
}
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
str_replace
没有在下列情况下工作:
$content = str_replace("$line_of_text[1]", "$icc", "$content");
摆脱'str_replace()'里面变量名称的引号' –
@JohnConde是正确的,它将文字字符“$ icc”和“$ content” –
谢谢我以前试过,但它没有工作。 – Max