how to store and retrieve array in mysql [closed]


Problem :

$ids = join(',',$galleries);  
$sql = "SELECT * FROM galleries WHERE id IN ($ids)";


Solution :

Looks like you're using the php script to construct a sql statement. I do this all the time. Try something like:

$galleryIds = implode(",",$galleries);

$sql = "SELECT * FROM galleries WHERE id IN ($galleryIds)";

Cheers and please vote me up!!

OK, Here's an edit because it seems that the Stack thinks you're going to use this for production code(I wouldn't).

Please use some kind of routine plus a database abstraction layer to deal with the escaping to prevent injection:

For instance, when getting your galleryIds from your database, run a check that they are all numeric data-types, and of course do something like Zend's db->quote($galleryIds) when using the variable in your sql construction.

For instance


Mysql Tutorials

Mysql Howto..

how to find record insert to mysql using commit()

How to merge data from mysql schemas that have diverged?

How to store out of default range dates in MySQL database?

How to sort MYSQL query to display alphabetically, with numbers last, and not including the word “The” in sort

How to Obtain Connection string for mysql with ubuntu server?

How to disable innodb in mysql?

How do I return only rows where all conditions are met based on an array of intergers (IDs)?

How can I accelerate this MySQL query?

How to check if a row exists which contains certain content in MySQL

how to use WHERE IN mysql stored procedure

how to add ' like special characters in mysql varchar data type column?

How to delete then insert data into mysql [closed]

How to persist mysql connection (mysql2 gem) with Ruby (no RoR)

How to use multiple forms to submit unique data PHP/MySQL

How to write a SQL query to generate a report in mysql

How to get numeric types from MySQL using PDO?

How to Select last Column in a row on mysql?

How to hook up mysql to my website?

How do I add to each row in MySQL?

How to use multiple database connections in laravel project?

How do I create a script to run MySQL command line?

How to optimize mysql fulltext union search?

Website form populated from mySQL database, how to return data

How to sort MySQL results based on a specific foreign value?

How to echo out table rows from the db (php)

How to implement listview by using aspx

How to dynamically show how many mysql queries have been executed on a page?

MySQL/Java - How can I do this SELECT statement?

How to change data type when running query (not inside PHP)

How to concatenate the output of a SQL query into one string