How to consult the database in angularjs?

0

I'm working with angularJS, a project created with YEOMAN-Generator!

and I need to consult a database.

I thought I could integrate php with a simple query but it still does not leave me. As I did? Well, here's the code:

Inside the folder app I created a folder called php and in it create a file that I called conection that contains the connection:

 <?php
$con = mysqli_connect("localhost", "root", "", "saltala");

?>

and another file called query.php :

     <?php

require_once 'conection.php';

$query = "SELECT rut, numero, servicio_id FROM Tickets";

$result = mysqli_query($con, $query);

$arr = array();

if(mysqli_num_rows($result) != 0) {

while($row = mysqli_fetch_assoc($result)) {

$arr[] = $row;}

}

echo $json_info = json_encode($arr);

?>

and in the controller folder where the project drivers are located, create the following code:

    //     comienza la  conexion

    getInfo();
function getInfo(){

$http.post('php/consulta.php').success(function(data){

$scope.details = data;

    console.log(data);
})};


//  finaliza la conexion

First run with gulp serve and you did not recognize at all the php , so change the location of the project folder to htdocs within xampp and I executed it and I threw the data I needed!.

Greetings.

    
asked by Hernan Humaña 11.10.2016 в 19:57
source

2 answers

1

Do not use the mysql functions, you are already depreciated. You need to use mysqli

link

And the problem is not angular, the problem is in the PHP code, I do not know what version of PHP you use, but it is possible that the problem is due to the library you are using. What is a fact, is that you are not establishing the connection to the database.

    
answered by 11.10.2016 в 20:04
1

You have a typo on the line echo json_encode($dato); must be echo json_encode($data); , reaclaro: $ data no $ data, now you must also hide the warnings error_reporting () error_reporting(E_ALL ^ E_WARNING); at the beginning of the php scope, for next questions please add the exact versions, since those libraries would be deprecated in the new versions.

    
answered by 11.10.2016 в 20:41