English 中文(简体)
将我的SQL数据库信息输入Java Array
原标题:Putting mySQL Database Information into a JavaScript Array

我试图做的是,通过gra取数据库信息并把它放入 j阵列,制造滑坡。 目前,我正在利用“jquery ajax”功能,把信息从单独的“Candre”档案中提出来。 这里是我的《加拿大刑法》:

mysql_connect( x ,  x ,  x ) or die( Not Connecting );
mysql_select_db( x ) or die ( No Database Selected );

$i = 0;
$sql = mysql_query("SELECT comicname FROM comics ORDER BY upldate ASC");

while($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
echo "comics[" .$i. "]= comics/" .$row[ comicname ]. " ;";
$i++;
}

What I want is to create the array in php from the mysql query and then be able to reference it with javascript in order to build a simple slideshow script. Please let me know if you have any questions or suggestions.

最佳回答

Ok有您的网址:echo json_encode(您的网址名称);

然后,在javascript一侧,你应该看一看这样的东西:

$.ajax({
        data:       "query string to send to your php file if you need it",
        url:        "youphpfile.php",
        datatype:   "json",
        success:    function(data, textStatus, xhr) {
                        data = JSON.parse(xhr.responseText);
                        for (i=0; i<data.length; i++) {
                        alert(data[i]); //this should tell you if your pulling the right information in

                        }
        });

可替换数据。 如果你获得正确数据,使用你的JSArray.push(数据);我相信,实际上会有更直接的方法......

问题回答

您不妨将所有浏览量排入一个大型阵列,然后将其编码为<代码>。 JSON :

$ret = array();
while($row = mysql_fetch_array($sql, MYSQL_ASSOC))
        $ret[] = $row
echo json_encode($ret);

然后,客户方面就这样说:

function mycallback(data)
{
        console.log(data[0].comicname); // outputs the first returned comicname
}

$.ajax
        (
        {
        url:  myscript.php ,
        dataType:  json ,
        success: mycallback
        }
        );

Upon successful request completion, mycallback will be called and passed an array of maps, each map representing a record from your resultset.

很难从您的提问中解答,但这样:

  1. You are making an AJAX call to your server in JS
  2. Your server (using PHP) responds with the results
  3. When the results come back jQuery invokes the callback you passed to it ...
  4. And you re lost at this point? ie. the point of putting those AJAX results in to an array so that your slideshow can use them?

如果是这样,解决办法非常简单:在你们的非洲复兴开发银行呼吁中,结果将变成一个全球变量(例如窗口)。 我的Results = 结果FromAjax;然后提到你希望开始幻灯时的变数。

然而,由于时间问题很可能是个问题,或许更有意义的是,实际上从你的反馈中开始幻灯。 作为一种额外奖金,这种办法并不需要全球变量。

如果是你 st的灯塔,请张贴更多的信息。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签