/    Sign up×
Community /Pin to ProfileBookmark

Using the INTO clause in a Select

I have the following:

[code]
$q = “set @fn = ‘/filmsdump.csv’;
select * from films where category_id = 1
order by 4
into @fn delimited by ‘,’;”;
echo “Running query <br>$q<br>”;
$qrslts = $pdo->query($q);
[/code]

First time I have tried this and despite reading what I could find I’m getting this error:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ‘select * from films where category_id = 1 order by 4 into @fn delimited by’ at line 2 in /home/albany/public_html/homejg/test.php:18 Stack trace: #0 /home/albany/public_html/homejg/test.php(18): PDO->query(‘set @fn = ‘/fil…’) #1 {main} thrown in /home/albany/public_html/homejg/test.php on line 18

Can someone correct my attempt for me?

to post a comment

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmauthorSep 14.2021 — Tried using the <> icon but doesn't seem to work here.
Copy linkTweet thisAlerts:
@NogDogSep 14.2021 — Wrapped the code in ... forum tags. :)

I don't think you can do multiple queries (separated by ;) in one $pdo-&gt;query(). I'm not familiar with that set @fn type of thing, so don't know off hand if this could be done somehow via separate queries (maybe within a transaction), or if it would be better to define a database stored function then call that, or...?

Of course, another alternative would be to run the select query, then loop through the result set and output to CSV via the PHP fputcsv() function, perhaps. 🤷
Copy linkTweet thisAlerts:
@ginerjmauthorSep 14.2021 — Yes of course but I thought this might come in handy now that I discovered it existed.

Managed to make it work a little bit in that now it give me an invalid authorization message that I think means that I am specifying a location that is now allowed. It's a my domain location that is being flagged so I guess I'll have to ask my host where mysql wants to save it and if I can then retrieve it.

Thanks for the quick reply

PS - I dropped the SET command since I discovered how to specify a filename directly.
Copy linkTweet thisAlerts:
@NogDogSep 14.2021 — @ginerjm#1637017 Cool...if you get it working, drop the final code here if you don't mind, and I can learn something. :)
Copy linkTweet thisAlerts:
@ginerjmauthorSep 15.2021 — I am told by my host that they cannot alter the "secure-file-priv" path that mysql uses to save files in. This is a shared sql server and that would not work well as it opens up security holes if they change it to something that my domain can reach.

So apparently the use of the INTO clause is not available on a shared server unless you are the one running it and not a client user.
×

Success!

Help @ginerjm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...