CudoX
CudoX

Reputation: 1003

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

I have moved to IIS 8 in PHP 5.4. I am trying to collect data from a table and insert them to a different one, i know my code is correct, but seems to be not working, probably because of the php version, can anyone help me?

here's my code

$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>";
}

thanks in advance.

Upvotes: 0

Views: 703

Answers (2)

webbiedave
webbiedave

Reputation: 48887

You're placing $rows['passwd'] inside of a double-quoted string. Instead you should do:

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

or:

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

or (I think this way is most readable):

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

If your column contains text you'll need to add surrounding single quotes where necessary.

Having said all that, you should instead use parameterized queries (if your database supports it) as it's safer (from SQL injection). If that's unavailable you will at the very least need to escape the data before concatenating it to the string.

Upvotes: 0

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

instead trying to insert one by one record, better to insert like below:

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

for more information :http://dev.mysql.com/doc/refman/5.5/en/insert-select.html

Upvotes: 1

Related Questions