Raja
Raja

Reputation: 63

How to get image file name from MySQL record?

I have images as records in question_answers table.

Images storing <span style="font-family:'MS Reference Sans Serif'"><span style="font-size:9pt"><img width="51" height="32" align="bottom" alt="mc001-2.jpg" src="@@PLUGINFILE@@/mc001-2.jpg" border="0"></span></span>

How to get image name mc001-2.jpg from here using PHP and MySQL?

enter image description here

Upvotes: 0

Views: 646

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Use preg_match():-

<?php

$str = '<span style="font-family:MS Reference Sans Serif"><span style="font-size:9pt"><img width="51" height="32" align="bottom" alt="mc001-2.jpg" src="@@PLUGINFILE@@/mc001-2.jpg" border="0"></span></span>';
preg_match('/alt="(.*?)"/', $str, $out);

//see first output
echo ($out[1]);

Output:-https://eval.in/399769

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_query('server name','user name','password','database name') or die(mysqli_connect_error());
$query = mysqli_query($conn,"Select answer from question_answers") or die(mysqli_error($conn));
$img_name_array = array();
if(mysqli_num_rows($query) >0){

    while ($row = mysqli_fetch_assoc($query)){
          $image_data = $row['answer'];
          preg_match('/alt="(.*?)"/',$str, $out);
          $img_name_array[] = $out[1];
    }

}else{

echo "no record exist"; 
}

Note:- $img_name_array will contain all the images name.Thanks.

Upvotes: 2

HenryTK
HenryTK

Reputation: 1287

SELECT image_name FROM question_answers
WHERE image_name LIKE '%mc001-2.jpg%';

If I have interpreted your question correctly it means you are storing HTML in your MySQL table. I hope I am wrong.

Upvotes: 0

Related Questions