-3

(Sorry for my bad English , it's not my mother tongue)

Ok so i want to extract datas from my Symfony DataBase (on Mysql) by using a JSON Script

here is the JSON script (connexion.php) for per example my DB : tblville

<?php $con = mysqli_connect("localhost","root","","tblville");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result= mysqli_query($con,"SELECT * FROM tblville");

while($row=mysqli_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysqli_close($con);
?>

The result : Data extracted using JSON on Table tblville

now i want to extract data from my db symfony using the same code :

<?php
$con = mysqli_connect("localhost","root","","symfony");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result= mysqli_query($con,"SELECT * FROM patients");

while($row=mysqli_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysqli_close($con);
?>

it doesn't work : JSON Script on Symfony

even if i change "localhost" by "127.0.0.1"

My paramater.yml code

parameters:
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: 1c9da92ddf2bff0482bf213b8f1cff6752c8b2b0

Need help , thanks

nikamanish
  • 662
  • 4
  • 17
  • In Symfony you must using doctrine to mapping entities to database tables and getting data. So You can't connect to database and execute queries like what you post – Mohamed Ben HEnda Apr 28 '16 at 22:35
  • i know , but i just want to extract them , and use the extracted data on android using JSON . – K.Chanceler Apr 28 '16 at 22:38
  • My Goal is to extract Data from my Database Symfony using a webservice JSON and show them on my android application (Eclipse Android) – K.Chanceler Apr 28 '16 at 22:41
  • So you should create an API, You can use FosRestbundle. – Mohamed Ben HEnda Apr 28 '16 at 22:43
  • each method in API, you gettin the data and you can construct json response – Mohamed Ben HEnda Apr 28 '16 at 22:45
  • Ok then i'll check that ! so there is no way for me to extract them data just by using my simple script on mysql , just like what i have done with the DB tblville ? is there a restriction ? thx by the way – K.Chanceler Apr 28 '16 at 23:03
  • If you decided to use simple script mysql then you don't need to developing with symfony, so you can create a simple scirpt php that connect to mysql, get data from tables and return a json response from these data – Mohamed Ben HEnda Apr 28 '16 at 23:11
  • Im developing with symfony because of my Project .(Operating room management) . One interface for The Administrator and One Interface for the secretary . And the other part of my project , is on android . The doctor will access data from an android application a view the list of patient for exemple , list of operations etc.. – K.Chanceler Apr 29 '16 at 00:20
  • So that's why i wanted to extract datas from that simple script , but i guess it wasn't the good way – K.Chanceler Apr 29 '16 at 00:24

1 Answers1

0

According to what I understood of your comments, you can use Doctrine to access to your data base and sens a json response from your controllor like this ;

class YourController extends Controller { 

  public function yourFunction() {

      //some code that lets you to connect database
      ...

     return new Response(json_encode($dataToReturn), 200, ['Content-Type' => 'application/json']);

  }

}

Otherwise, try to look at this post

Good luck ^^!

Community
  • 1
  • 1
Houssem ZITOUN
  • 644
  • 1
  • 8
  • 23