如何在SYMFONY 3上使用JSON从DB Mysql中提取数据

时间:2016-04-28 22:29:19

标签: php mysql json symfony

(抱歉我的英语不好,这不是我的母语)

好的,我想通过使用JSON脚本从我的Symfony DataBase(在Mysql上)中提取数据

这里是JSON脚本(connexion.php),例如我的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);
?>

结果: Data extracted using JSON on Table tblville

现在我想使用相同的代码从我的db symfony中提取数据:

<?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);
?>

它不起作用:JSON Script on Symfony

即使我将“localhost”改为“127.0.0.1”

我的paramater.yml代码

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

需要帮助,谢谢

1 个答案:

答案 0 :(得分:0)

根据我对您的评论的理解,您可以使用Doctrine访问您的数据库并感受您的控制者的json响应;

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']);

  }

}

否则,请尝试查看此post

祝你好运^^!