0

Hello everyone is there any possibly to fetch data from mysql databases in javascript . For example i have the following code

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>


        <!-- 1. Add these JavaScript inclusions in the head of your page -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript" src="../js/highcharts.js"></script>

        <!-- 1a) Optional: add a theme file -->
        <!--
            <script type="text/javascript" src="../js/themes/gray.js"></script>
        -->

        <!-- 1b) Optional: the exporting module -->
        <script type="text/javascript" src="../js/modules/exporting.js"></script>


        <!-- 2. Add the JavaScript to initialize the chart on document ready -->
        <script type="text/javascript">


            Highcharts.setOptions({
                global: {
                    useUTC: false
                }
            });

            var chart;
            $(document).ready(function() {
                chart = new Highcharts.Chart({
                    chart: {
                        renderTo: 'container',
                        defaultSeriesType: 'spline',
                        marginRight: 10,
                        events: {
                            load: function() {

                                // set up the updating of the chart each second
                                var series = this.series[0];
                                setInterval(function() {
                                    var x = (new Date()).getTime(), // current time
                                        y = Math.random();
                                    series.addPoint([x, y], true, true);
                                }, 500);
                            }
                        }
                    },
                    title: {
                        text: 'Live random data'
                    },
                    xAxis: {
                        type: 'datetime',
                        tickPixelInterval: 150
                    },
                    yAxis: {
                        title: {
                            text: 'Value'
                        },
                        plotLines: [{
                            value: 0,
                            width: 1,
                            color: '#808080'
                        }]
                    },
                    tooltip: {
                        formatter: function() {
                                return '<b>'+ this.series.name +'</b><br/>'+
                                Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ 
                                Highcharts.numberFormat(this.y, 2);
                        }
                    },
                    legend: {
                        enabled: false
                    },
                    exporting: {
                        enabled: false
                    },
                    series: [{
                        name: 'Random data',
                        data: (function() {
                            // generate an array of random data
                            var data = [],
                                time = (new Date()).getTime(),
                                i;

                            for (i = -19; i <= 0; i++) {
                                data.push({
                                    x: time + i * 1000,
                                    y: Math.random()
                                });
                            }
                            return data;
                        })()
                    }]
                });


            });

        </script>

    </head>
    <body>

from this code

   data: (function() {
                            // generate an array of random data
                            var data = [],
                                time = (new Date()).getTime(),
                                i;

                            for (i = -19; i <= 0; i++) {
                                data.push({
                                    x: time + i * 1000,
                                    y: Math.random()
                                });
                            }
                            return data;

instead of random no i want to fetch from the database . please guide

3
  • Is it running on PHP in the backend? Commented Jul 18, 2011 at 19:51
  • @webarto sorry for that , but i am wondering might be there are some methods Commented Jul 18, 2011 at 19:52
  • 1
    @umar, you can make JSON with PHP (from database) and then fetch and manipulate it with Javascript. But directly, no. Commented Jul 18, 2011 at 19:54

3 Answers 3

1

You can't connect to a MySQL database from traditional Javascript. You'll have to do that server-side, such as in PHP.

There are some in-between solutions, such as Jaxer, (A tutorial here.), but this would probably be a bit over your head.

Sign up to request clarification or add additional context in comments.

Comments

1

No, there's (fortunately) no express-way from JavaScript to MySQL. You'd have to make requests against some custom HTTP-based service (written in whatever programming language) that then accesses MySQL.

Comments

1

Make an AJAX request to the PHP script (using jQuery or whichever way you prefer), and get the PHP script to give you back JSON data of the row using json_encode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.