无法使用php和ajax从数据库中获取数据

时间:2017-01-28 04:17:49

标签: php css mysql sql ajax

我正在使用 bootstrap php ajax 。我似乎无法弄清楚问题。

另外我注意到当我把它输入我的记事本++时,我的mysqli_connect没有变成蓝色。这是正常的吗?

我在index.php中的代码

          <div class="col-md-2">
           <div id="get_category">
           </div>
           //There is a code here of my < li > which are my categories
           </div>

这是我在main.js

中的代码
 $(document).ready(function(){

 cat();
 function cat(){
 $.ajax({
 url     : "action.php"
 method  : "POST",
 data    : {category:1},
 success : function(data){
         $("#get_category").html(data);
    }

    })  


  }


})

这是我的db.php

   <?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $db = "gadsdb";

 $con = mysqli_connect($servername, $username, $password, $db);

 if(!$con){
 die("Connection failed: " . mysqli_connect_error());


 }

?>

我的行动代码。 PHP

     <?php
   include "db.php";
  if(isset($_POST["category"])){
    $category_query = "SELECT * FROM categories";
    $run_query = mysqli_query($con,$category_query);
     echo"
     <div class='nav nav-pills nav-stacked'>
     <li class='active'><a href='#'><h4>Categories</h4></a></li>

     ";
        if(mysqli_num_rows($run_query) > 0){
       while($row = mysqli_fetch_array($run_query)){
         $cid = $row["cat_id"];
         $cat_name = $row["cat_title"];

            echo"

           <li><a href='#'>$cat_name/a></li>

            ";

          }
         echo "</div>";

      }

    }

2 个答案:

答案 0 :(得分:0)

改变这行代码:

$con = my_sqli_connect($servername, $username, $password, $db);

要:

$con = mysqli_connect($servername, $username, $password, $db);

答案 1 :(得分:0)

function cat(){
    $.ajax({
    type: 'POST',
    url: 'action.php',
    data: {category:1},
    success: function(data){
         $("#get_category").html(data);
    }
 })
}
$(document).ready(function(){
 cat();
})