如何使用php mysql json从url中基于行id从数据库中检索值?

Hi I am new to PHP JSON

My URL

http://xxxx.in/event-view.html?event_id=8

My php code (json-eventview.php)

    include "db.php";
    $data = array();
    $id = $_GET['event_id'];
    $q=mysqli_query($con,"select * from `ca_events` where ca_events.id=$id ");
    while ($row=mysqli_fetch_object($q)){
    $data[]=$row;
    }
    echo json_encode($data);

and Javascript (get Json) used in above url.

    $(document).ready(function() {
    var url = "http://localhost/php/json-eventview.php";
    $.getJSON(url, function(result) {
        console.log(result);
        $.each(result, function(i, field) {
            var id = field.id;
            var general_id = field.general_id;
            var name = field.name;
            var description = field.description;
            var agenda = field.agenda;
            var date = field.date;
            var time = field.time;
            $(".navbar-brand span").append(" " + name + " ");
        });
    });
});

the above gives an empty space.

If, I remove $id = $_GET['event_id']; and WHERE in php file I get the entire table as result.

I need the column values of that particular id in url. can anyone help or give other ideas and examples to get the values based on id from url.

Your issue is that you are passing GET data to an HTML form (Not a PHP or something else)

You need to add some jquery (Since I see Jquery code in your javascript) to pull out the event_id =8 Such as this location How to Read a URL from Jquery

Then Add it to your to the end of your .getJson URL.

Change Javascript Code to this :

$(document).ready(function() {
var url = "http://localhost/php/json-eventview.php?event_id=YOUR_REQUESTED_ID";

and put your id insted of YOUR_REQUESTED_ID

As I mentioned in my comment above, the problem is that the url parameter is on the page that has your JavaScript, and not on the PHP page that is receiving the AJAX call. One way to resolve your problem would be to pass the event_id to your PHP page that receives the AJAX call.

On your page that has the JavaScript, parse the url to get the event_id parameter.

The function from this site will parse your url for parameters:

$(document).ready(function() {
  var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
  };
  var eventId = getUrlParameter('event_id');
});

This will give you the event_id so that you can then use it in your AJAX call to the other PHP page:

var url = 'http://localhost/php/json-eventview.php?event_id="' + id + '"';

Your complete code should look something like this:

$(document).ready(function() {
  var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
  };
  var eventId = getUrlParameter('event_id');
  if(eventId !== undefined {
   var url = 'http://localhost/php/json-eventview.php?event_id="' + eventId + '"';
   $.getJSON(url, function(result) {
    console.log(result);
    $.each(result, function(i, field) {
        var id = field.id;
        var general_id = field.general_id;
        var name = field.name;
        var description = field.description;
        var agenda = field.agenda;
        var date = field.date;
        var time = field.time;
        $(".navbar-brand span").append(" " + name + " ");
    });
   });
  }
});