php在5.4版上将fetch数组中的数据插入到其他表中


php insert data from fetch array to other table on version 5.4

我已经转到PHP 5.4中的IIS 8。我正试图从一个表中收集数据,并将它们插入另一个表,我知道我的代码是正确的,但似乎不起作用,可能是因为php版本的原因,有人能帮我吗?

这是我的代码

$query = odbc_exec($conn, "SELECT * FROM member");
while($rows = odbc_fetch_array($query)) {
$querystring = "INSERT INTO oldusers (username, password, regdate) VALUES ('$rows['userid']', '$rows['passwd']', '$rows['registdate']')";
$query2 = odbc_exec($conn, $querystring);
odbc_free_result($query2);
//echo $rows['userid']." ".$rows['passwd']." ".$rows['registdate']."<br>";
}

提前谢谢。

与其尝试逐个插入记录,不如像下面这样插入:

INSERT INTO oldusers (username, password, regdate) SELECT userid,passwd,registdate FROM member

有关详细信息:http://dev.mysql.com/doc/refman/5.5/en/insert-select.html

您将$rows['passwd']放在一个双引号字符串中。相反,你应该这样做:

$str = "some sql $rows[passwd] rest of sql"; // notice the absence of single quotes

或:

$str = "some sql {$rows['passwd']} rest of sql";

或者(我认为这种方式最可读):

$str = 'some sql' . $rows[passwd] . ' rest of sql';

如果您的列包含文本,则需要在必要时添加环绕的单引号。

话虽如此,您应该使用参数化查询(如果您的数据库支持它),因为它更安全(来自SQL注入)。如果这不可用,那么在将数据连接到字符串之前,您至少需要转义数据。