2

I am trying to add image in MAMP server table & fetch it in PHP, and create json. But I am not getting how to add image in table and again it in file. I am new in PHP scripting. Please someone provide me right direction. I have added my PHP code & also MAMP table screenshot.

PHP Code file

<?php
$conn    = mysql_connect("localhost:8888","asmita","asmita123") or die(mysql_error());
if($conn)
{
    mysql_select_db("EmployeeInfo");
    //echo "connected";
    //echo $_SERVER['DOCUMENT_ROOT'];
}
else
{
    echo "not connected";
    mysql_error();
}
$selectQuery="select * from Emp";
$row=mysql_query($selectQuery);
while($result=mysql_fetch_array($row))

{
//echo $_SERVER['http://localhost:8888/images/index.jpg'];

    //output need in JSON format for webservice ...
    $empname= $result['Name'];
    $empadd= $result['Address'];
    $emppho= $result['Phone'];
    $emppost= $result['Post'];
    $empphoto=$result['Photo'];
    $jsonArray[]=array("name"=>"$empname","address"=>"$empadd","phone"=>"$emppho","post"=>"$emppost","photo"=>"$empphoto");

}
echo json_encode($jsonArray);

  ?>

MAMP table data

enter image description here

Here I added image path. Is this correct? Thanks.

NightFury
  • 13,436
  • 6
  • 71
  • 120
Asmita
  • 477
  • 8
  • 20
  • 1
    Take a look of this url http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code – er.irfankhan11 May 30 '16 at 11:15
  • nop its not like that @Irfan i m going to add first image in my mamp server table how i add it? & then will write code for it in my PHP file for select all data of that table. how i do this – Asmita May 30 '16 at 12:18

1 Answers1

1

HTML Code

<input type="file" name="img_file" id="imageUpload">

PHP Code

$filename = $_FILES['img_file']['name'];
$src = $_FILES['img_file']['tmp_name'];
$folder = "/images/" ; 

    $move= move_uploaded_file($src,"$folder/".$image);
    if($move!=false)
    {
     $pic   = "http://localhost:8888/directoryName/images" .$filename; //This variable insert into Photo column
      $query="Insert or Update Query for your table";
      $rslt = mysql_query($query);
    }

$data=array();
$selectQuery="select * from Emp";
$row=mysql_query($selectQuery);
while($result=mysql_fetch_array($row))
{
    $data[] = $result;
}
echo json_encode($data);
er.irfankhan11
  • 1,280
  • 2
  • 16
  • 29
  • Yes its right @Irfan but i am going to add image in mamp server table then for that what kind data type i need to take and in mamp server in where i will store that images or what i do . I want that image from mamp server table in PHP script. Sorry i am new in PHP so have not idea about all this stuffs. – Asmita May 30 '16 at 14:59
  • By the `$pic` variable you can store the path of image into database, And whenever you need fetch the image path from database and use in html image tag – er.irfankhan11 May 31 '16 at 05:10