Forum Moderators: coopster

Message Too Old, No Replies

Fun with literals

Problem with variable in database query.

         

SBro

3:42 am on Jun 1, 2004 (gmt 0)

10+ Year Member



Hi all, I have the following code:

<?php
...
$upfile = '../uploads/'.$userfile_name;
$id = $HTTP_GET_VARS["id"];

$sql = 'LOAD DATA LOCAL INFILE \'$upfile\' INTO TABLE `fund_$id` FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'( `first_n` , `last_n` , `class` , `product` )';

...
?>

The problem being that when I execute this script I get the error that tablename fund_$id doesn't exist...obviously it is not reading the variable $id properly. I'm led to believe that it's reading it literally (obviously) and not using the value of $id. How must this be formatted so that it does read it? thankyou.

Swash

3:51 am on Jun 1, 2004 (gmt 0)

10+ Year Member



$ourtable = "fund_" . $id;

$sql = 'LOAD DATA LOCAL INFILE \'$upfile\' INTO TABLE \'$ourtable\' FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'( `first_n` , `last_n` , `class` , `product` )';

should do the trick

SBro

4:12 am on Jun 1, 2004 (gmt 0)

10+ Year Member



Unfortunately this didn't work for me either, I got the following error:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''$ourtable' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY

I have now got it reading the variable with the following:

$sql = 'LOAD DATA LOCAL INFILE ' . $upfile . ' INTO TABLE ' . $ourtable .' FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\'( `first_n` , `last_n` , `class` , `product` )';

However I now get another error:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '../uploads/Book1.csv INTO TABLE fund_7 FIELDS TERMINATED BY ','

Not really sure what this error could be, since I got the code straight from PHPMyAdmin.