如何从MySQL表中获取所有结果?

时间:2014-12-16 07:08:35

标签: javascript php mysql angularjs

我正在尝试从phpadmin mysql表中获取所有结果。问题是使用下面的代码与php和angular JS,每次只获取第一行。假设表中有5个条目。发生的事情只是第一个在网站上显示5次。当我做一个php echo(在代码中注释掉)时,它返回表中的所有结果,但是它不能用于角度JS。

我也在使用JS模糊搜索库,由于空间问题我在这里没有提供。这是代码:

</table>        
<?php

          $result = mysql_query("SELECT * FROM user_files ORDER BY id DESC");  
          while($row = mysql_fetch_array($result)){

          $id1 = $row['id'];
          $name = $row['user_file_name'];
          //echo $name.'<br/>';                     

?>

<div class="container" ng-init="books=[{book:'<?php echo $name;?>',author:'<?php echo $id1;?>'}]">
</div>                                


<tr ng-repeat="bookInfo in books | filter:bookText | orderBy:'book'">                                   


<td style="color: black;">{{ bookInfo.book }} - {{ bookInfo.author | lowercase}}</td>

<td style="color: black;" align="center">Size</td>
<td style="color: black;" align="center">Type</td>

<td style="color: black;" align="center" width=90%> 
<input type="button" name="read" value="Read"></td>

<td style="color: black;" align="center" width=90%>
<input type="button" name="write" value="Write"></td>

<td style="color: black;" align="center" width=90%>
<input type="button" name="download" value="Download"></td>

<td style="color: black;" align="center" width=90%>
<input type="button" name="share" value="Share"></td>

</tr>

3 个答案:

答案 0 :(得分:0)

因为你的while循环只执行了一次,所以你必须改变while循环逻辑。

答案 1 :(得分:0)

是的,因为您将数组初始化为

ng-init="books=[{book:'<?php echo $name;?>',author:'<?php echo $id1;?>'}]

编译后显示类似

的内容
ng-init="books=[{book:'bookName1',author:'authorId1'}]

然后只有一本书适用于ng-repeat

所以你需要做的是,

<?php

  $result = mysql_query("SELECT * FROM user_files ORDER BY id DESC");  
  $dataArr = array();         // to get the all the detail to a array
  while($row = mysql_fetch_array($result)){
      $id1 = $row['id'];
      $name = $row['user_file_name'];
      array_push($dataArr , array("book"=>$name , "author"=>$id1));
  }
 ?>

... ng-init="books=<?php echo json_encode($dataArr); ?>">

答案 2 :(得分:0)

首先通过从表中获取所有数据并将其转换为json数组来创建数组。注意:还要定义角度js控制器&#34; appController&#34;,

      <?php
        $books = array();
        $result = mysql_query("SELECT * FROM user_files ORDER BY id DESC");  
        $i = 0;
        while($row = mysql_fetch_array($result)){

          $books[$i]['id'] = $row['id'];
          $books[$i]['name'] = $row['user_file_name'];
          $books[$i]['bookText'] = $row['bookText'];
          $books[$i]['author'] = $row['author'];
          $i++;
        }

        $books = json_encode($books);
      ?>

现在将此json数组分配给angular books数组

      <script>
        function appController($data) {
          $data.books = "<?php echo $books; ?>" ;
        }
      </script>