title: PHP code to upload image to MySQL and display in Json
body: <p>Am trying to upload image(blob) to database and display image using php. To display all the information am using json_encode.Below u can see the output</p>
  
  <pre><code>{"image":[{"image_id":"1","title":"BMW X1","image_name":"Foursquare-icon.png","image":null,"price":"300","description":"test 1"}
  </code></pre>
  
  <p>The problem here is am getting the image path not the url. can anyone tel me how to get the image url.I want something similar to this</p>
  
  <pre><code> {"countries":[{"countryname":"India","flag":"http:\/\/wptrafficanalyzer.in\/p\/demo1\/india.png","language":"Hindi","capital":"New Delhi","currency":{"code":"INR","currencyname":"Rupee"}}
  </code></pre>
  
  <p>Below is my PHP code to get the data.</p>
  
  <pre><code> mysql_connect("localhost","root","");
   mysql_select_db("database");
   if(isset($_GET['id'])){
  
      $id = mysql_real_escape_string($_GET['id']);
      $query = mysql_query("SELECT * FROM `testblob`");
  
      $response_array = array();
      $pic_array = array();
  
      while ($row = mysql_fetch_assoc($query) ) {
  
          $data['image_id'] = $row["image_id"];
      $data['title'] = $row["title"];
  
          $data['image_name'] = $row["image_name"];
      $data['image_type'] = $row["image_type"];
      $data['image'] = $row["image"];
      $data['price'] = $row["price"];
      $data['image_size'] = $row["image_size"];
      $data['image_ctgy'] = $row["image_ctgy"];
  $data['description'] = $row["description"];
  
          array_push($pic_array, $data);
      }
  
      $response_array = array('image' =&gt; $pic_array);
  
      echo json_encode($response_array);
  
  }else{
      echo "Error!";
  
  }**
  </code></pre>
  
  <p>Below is my upload PHP file</p>
  
  <pre><code>&lt;?php
  
  if(!isset($_FILES['userfile']))
  {
  echo '&lt;p&gt;Please select a file&lt;/p&gt;';
  }
  else
  {
  try    {
      upload();
      /*** give praise and thanks to the php gods ***/
      echo '&lt;p&gt;Thank you for submitting&lt;/p&gt;';
      }
  catch(Exception $e)
      {
      echo '&lt;h4&gt;'.$e-&gt;getMessage().'&lt;/h4&gt;';
      }
  }
  
  function upload(){
  /*** check if a file was uploaded ***/
  if(is_uploaded_file($_FILES['userfile']['tmp_name']) &amp;&amp; getimagesize($_FILES['userfile']['tmp_name']) != false)
  {
  /***  get the image info. ***/
  $size = getimagesize($_FILES['userfile']['tmp_name']);
  /*** assign our variables ***/
  $type = $size['mime'];
  $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
  $size = $size[3];
  $name = $_FILES['userfile']['name'];
  $maxsize = 99999999;
  
  
  /***  check the file is less than the maximum file size ***/
  if($_FILES['userfile']['size'] &lt; $maxsize )
      {
      /*** connect to db ***/
      $dbh = new PDO("mysql:host=localhost;dbname=swapmeet", 'root', '');
  
              /*** set the error mode ***/
              $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
          /*** our sql query ***/
      $stmt = $dbh-&gt;prepare("INSERT INTO testblob (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)");
  
      /*** bind the params ***/
      $stmt-&gt;bindParam(1, $type);
      $stmt-&gt;bindParam(2, $imgfp, PDO::PARAM_LOB);
      $stmt-&gt;bindParam(3, $size);
      $stmt-&gt;bindParam(4, $name);
  
      /*** execute the query ***/
      $stmt-&gt;execute();
      }
  else
      {
      /*** throw an exception is image is not of type ***/
      throw new Exception("File Size Error");
      }
  }
  else
  {
  // if the file is not less than the maximum allowed, print an error
  throw new Exception("Unsupported Image Format!");
  }
  }
  
  ?&gt;
  
  &lt;img src="imageshow.php?id=1"&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>