Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am writing a little script to read files in a folder (photos) returning an array of file name and file size. Can mysqli handle a prepared statement inserting an array? Would be nice to insert 500 photo names at once rather than typing them all in.


Ps.. Not for a public site, just a script i run on a home server to build a site.


Link to post
Share on other sites

Yes, MySQLi prepared statement functions can handle arrays with a variable number of arguments, but doing so can be a bit tricky.


You have to use the call_user_func_array function (http://us2.php.net/manual/en/function.call-user-func-array.php) to call the mysqli_stmt_bind_param function, etc. with an array containing a variable number of arguments.

The thing that gets tricky is that those MySQLi functions require references to values (not values) to be properly called.


Try playing with it a bit, and if you're totally stuck, please let me know, and I'll help out more.



By the way, what is your avatar of? It looks like a picture taken from on top of a horse, but I can't tell.

  • Upvote 1
Link to post
Share on other sites

Avatar is a carriage horse. So I am behind but up high enough to see across his back. I am a horseman and artist. Have not been programming for long.


I will study this. Maybe my problem has been in presenting a correctly formatted array. Array must be two dimensional, correct? Using SPL to get file name and file size. Just my first time using these functions. I have stuck to procedural code to date though i have been reading oop. Knowing it is possible helps a lot!!!


Thank you. I will post some code if I get super stuck.

Link to post
Share on other sites

You don't need SPL to get file names and sizes.

I'd simply use the scandir function (http://php.net/manual/en/function.scandir.php) to get all the files in a directory, and then use the filesize function (http://php.net/manual/en/function.filesize.php) to get the sizes of all the files.

That should be all you need for that.


As for the array, you'll need a one-dimensional array with a lot of values, if you want to insert info about 500 files at once.

If this is going to be a one-time thing, you may just want to insert info about one file per query, and then execute the same query with different values 500 times. That's actually one of the main benefits of prepared statements.

Also, going that route will allow you to avoid having to use the call_user_func_array function, which can be truly confusing.


Anyway, again, best of luck, and come back if you're stuck.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...