【问题标题】:Fetching data from local mysql using angular not working使用角度无法从本地mysql获取数据
【发布时间】:2015-08-28 06:41:52
【问题描述】:

我一直在尝试从本地服务器从 MySql 获取数据。我的本地服务器是 WAMP。

这里是我的 index.php

<html lang="en-US">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body ng-app='myApp'>
<div ng-controller="customersCtrl">
    <table>
    <thead><tr><th>ID</th><th>Name</th><th>Email</th></tr></thead>
    <tbody>
        <tr ng-repeat="user in names">
            <td>{{user.id}}</td>
            <td>{{user.name }}</td>
            <td>{{user.city}}</td>
        </tr>
    </tbody>
    </table>
</div>  
<script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {
        $http.get("customers_mysql.php")
        .success(function (response) {$scope.names = response.records;});
    });
</script>
</body>
</html>

这是我的customers_mysql.php

$conn = new mysqli("localhost", "root", "", "test");
$sql = "SELECT name, city, country FROM customers";
$results = mysqli_query($conn, $sql);
$data = array();

while($row = mysqli_fetch_assoc($results)) {
    $data[] = $row;
}

$json = json_encode( $result );
echo $json;

当我加载 index.html 页面时,我根本没有得到任何结果。 所以我认为我的 sql 有问题所以我创建了一个新的 php 页面

调用 textCustomers.php

<?php
    include('customers_mysql.php');
    $results = mysqli_query($conn, $sql);


    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else {
        if (mysqli_num_rows($results) > 0) {
            while($row = mysqli_fetch_assoc($results)) {
                echo '<tr>';
                echo '<td>' . $row['name'] . '</td>';
                echo '</tr>';
            }
        }
    }

?>

这行得通。

无论如何,我一直在关注 stackoverflow、w3 和其他网站的代码,但它们都不起作用。我做错了什么?

谢谢

【问题讨论】:

  • php.ini 中是否启用了 mysqli 扩展?

标签: php mysql angularjs localhost


【解决方案1】:

试试这样的:

$conn = new mysqli("localhost", "root", "", "test");
$sql = "SELECT id, name, city FROM customers";
$results = mysqli_query($conn, $sql);
$data = array();

while($row = mysqli_fetch_assoc($results)) {
    $data[] = $row;
}

$json = json_encode( $data );
echo $json;

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2022-01-06
    • 2018-11-20
    • 2017-02-05
    • 2021-12-20
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2021-04-16
    相关资源
    最近更新 更多