Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Bind jQuery Grid to MySQL Database using PHP

, 19 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to connect a jQuery Grid to a MySQL database using PHP.

In this post we will show you how to connect our jQuery Grid to MySql Database using PHP. We will obtain the data from MySQL database and especially the Northwind database. You can download the Northwind database .sql script here and run it into MySQL to create the database.

The first thing we need to do is create the file we’ll connect with. We’ll call this file connect.php.

<?php
# FileName="connect.php"
$hostname = "localhost";
$database = "customers";
$username = "root";
$password = "";
?>

Now we have our file to do the connection for us and we need to create the file that will run the query and bring the data so our Grid can be populated. We will call the file data.php.

<?php
#Include the connect.php file
include('connect.php');
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die('Could not connect: ' . mysql_error());
//select database
mysql_select_db($database, $connect);
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
   print "can't find $database";
}
// get data and store in a json array
$query = "SELECT * FROM customers";
$from = 0;
$to = 30;
$query .= " LIMIT ".$from.",".$to;

$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $customers[] = array(
        'CompanyName' => $row['CompanyName'],
        'ContactName' => $row['ContactName'],
    'ContactTitle' => $row['ContactTitle'],
    'Address' => $row['Address'],
    'City' => $row['City']
      );
}

echo json_encode($customers);
?>

The data is returned as JSON. This is it for the connection and data gathering. Let’s see how to add the data we just gathered into our jQuery Grid. Create the index.php file and add references to the following JavaScript and CSS files.

<link rel="stylesheet" href="styles/jqx.base.css" type="text/css" />
<link rel="stylesheet" href="styles/jqx.classic.css" type="text/css" />
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jqxcore.js"></script>
<script type="text/javascript" src="jqxbuttons.js"></script>
<script type="text/javascript" src="jqxscrollbar.js"></script>
<script type="text/javascript" src="jqxmenu.js"></script>
<script type="text/javascript" src="jqxgrid.js"></script>

Create a div tag for the Grid.

<div id="jqxgrid"></div>

Create your grid and load the data. We define a source object for the Grid and bind that source to the data.php which returns the JSON data. We are also defining the Grid columns for the CompanyName, ContactName, ContactTitle, Address and City.

<script type="text/javascript">
    $(document).ready(function () {
        // prepare the data
        var source =
        {
            datatype: "json",
            datafields: [
                { name: 'CompanyName'},
                { name: 'ContactName'},
                { name: 'ContactTitle'},
                { name: 'Address'},
                { name: 'City'},
            ],
            url: 'data.php'
        };

        $("#jqxgrid").jqxGrid(
        {
            source: source,
            theme: 'classic',
            columns: [
              { text: 'Company Name', datafield: 'CompanyName', width: 250},
              { text: 'ContactName', datafield: 'ContactName', width: 150 },
              { text: 'Contact Title', datafield: 'ContactTitle', width: 180 },
              { text: 'Address', datafield: 'Address', width: 200 },
              { text: 'City', datafield: 'City', width: 120 }
          ]
        });
    });
</script>

The result is a nice looking jQuery Grid.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jqwidgets
jQWidgets
United States United States
jQWidgets specializes in the development of platform independent and cross-browser compatible presentation layer components for building modern web-based applications for PC, Touch and Mobile. Our product provides developers with the essential set of User Interface widgets needed to streamline their development and reduce project costs.
Our goal is to help our customers deliver better web-based applications that can be accessed through any device and are pleasure to use.
Group type: Organisation

13 members

Follow on   Twitter

Comments and Discussions

 
QuestionSome troubles Pinmemberhowitworks13-May-12 2:08 
Questionnot getting jgrid Pinmember1604198414-Mar-12 23:16 
AnswerRe: not getting jgrid Pingroupjqwidgets14-Mar-12 23:19 
You can download the full source code of the article from here: http://www.jqwidgets.com/download/[^]. The source code implementation is in the php_demos folder in the archive.
You can download the Northwind Database used in the article from here: http://code.google.com/p/northwindextended/downloads/detail?name=Northwind.MySQL5.sql[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 19 Jan 2012
Article Copyright 2012 by jqwidgets
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid