0

I Have a problem to select a part of characters. Here's my field's content :

[{"index":1,"image":"un-nouveau-test-d-annonce_12_1.jpg","thumbnail":"un-nouveau-test-d-annonce_12_1_t.jpg"}]

I want to select un-nouveau-test-d-annonce_12_1_t.jpg only. But, the name is different in all fields.

field1 : un-nouveau-test-d-annonce_12_1_t.jpg
field2 : hello_ads_2.jpg

So, I want that my "select" select the first letter (u or h) to the end of the address (jpg in all cases).

I have this :

$img = "SELECT
    SUBSTR(images, LOCATE('l\":\"',images)+1, 
    (CHAR_LENGTH(images) - LOCATE('l\":\"',REVERSE(images)) - LOCATE('l\":\"',images))) 
    FROM  mq74m_adsmanager_ads";

$resultimg = mysql_query($img) or die(mysql_error());
if (mysql_num_rows($resultimg) > 0)
{
    while ($rowimg = mysql_fetch_array($resultimg))
    {
        echo $rowimg['images'];
    }
}

But the result is empty.

1
  • 1
    This is the sign that you should normalize your database :)
    – Ja͢ck
    Nov 26, 2012 at 14:11

1 Answer 1

0

Extract all from database, then apply json_decode and you will obtain an array. After that you can do $result[$i]['image'] where $i is the index.

2
  • Thanks for your fast answer. How to use json_decode with MYSQL ? Nov 26, 2012 at 15:05
  • $query = "SELECT images FROM table"; $results = mysql_query($query); while ( $row = mysql_fetch_assoc($results)) { $decoded = json_decode($row['images'], TRUE); // TRUE is put to return an array echo $decoded['image']; } Nov 26, 2012 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.