Reputation: 11
One of my fields in my data base is an array which has been converted to a string using the implode()
function.
How do I retrieve the contents of this field (LESSONS
) from the database and store it to a string when a user entered value is equal to the the value of the field NAME
?
Thanks in advance for any help you may be able to provide.
Upvotes: 0
Views: 159
Reputation: 197599
If you used the implode function to convert your array into a string, then this data has lost any information about the array keys.
As far as you want to convert the data back, use the explode function:
$array = explode(',', $columnData);
But You can therefore not search for array keys within the database.
Next to that, the MySQL database (I assume you're using MySQL) can not search for array keys anyway.
You need to store the data in some other way into the mysql to search for it in an SQL later on.
For example, you can create a table that stores key/value combinations with a grouping index.
However MySQL has some string functions that can help you searching within the (now) string data in the MySQL database.
When searching for a value, before the comparison add a comma at the beginning and one at the end of the string. There is a MySQL string function that can concatenate strings. Then search within that expression for your value with a comma added in front and back as well.
Then you can lookop a single array element within the mysql database. MySQL String Functions.
This is a quick solution only, this won't work on large databases performant. However it might solve your problem w/o changing your database structure.
Upvotes: 0
Reputation: 17
I don't know if I understood your question but... Take a look about Stored Procedures
Upvotes: 0
Reputation: 2398
Here you go:
$r = mysql_query('SELECT LESSONS FROM TABLE WHERE NAME=\'user_string\'');
$rows = mysql_fetch_assoc($r);
echo $rows['LESSONS'];
Upvotes: 1