Jump to content

Recommended Posts

I have a query where I need to select an ID from a row where a column has an array.

for instance I have

ID Name photos

1 My Name 300,302,303,

2 2nd Name 304,405,309,

 

How would I grab the ID where the "photos" contains 405,?

But it can't select where it contains 1405 or 4054, or anything like that so I can't use LIKE %405% to select this.

 

$sql = "SELECT ID FROM user_images WHERE photos LIKE '%405%'";
					 $sql_query = mysql_query($sql);
					 $true_row = mysql_fetch_assoc($sql_query);

Link to comment
https://forums.phpfreaks.com/topic/269096-sql-and-an-array/
Share on other sites

Naw, I have a ton of other tables for things like that, there has to be a way to pull an array out this way. Anyone with any ideas? I know how to easily pull it by grabbing it, exploding it by the , and using a foreach. I also put them in other tables with these id's, but for commenting and other purposes to keep them together I have to do it this way for this table.

 

It's as easy as this to grab them, but what about searching right inside the query?

$uploads= $true_row['photos'];
$s = explode(",", $uploads);
foreach($s as $a)
{
echo "$s";
}

Edited by ebolt007
Link to comment
https://forums.phpfreaks.com/topic/269096-sql-and-an-array/#findComment-1382780
Share on other sites

Actually, there are many reasons to store serialized data -- but very few beginners have *good* reasons to do so.

 

Short answer: if you're even going to need to query, join on, or change, an individual piece of this 'array', then it should not be an array.

Link to comment
https://forums.phpfreaks.com/topic/269096-sql-and-an-array/#findComment-1383509
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.