【问题标题】:Ajax MySQL "Unexpected token <"Ajax MySQL“意外令牌<”
【发布时间】:2015-04-11 14:29:30
【问题描述】:

我想做简单的事情,从 MySQL 数据库中获取表。 早些时候我没有 MySQLi 代码,简单的 MySQL 并且它正在工作。现在,更改为 MySQLi 后出现错误:

Uncaught SyntaxError: Unexpected token

我有这个代码:

AJAX:

("#load-all").click(function(){
    $.ajax({   
        type: "GET",
        url: "akceptAPI.php",             
        dataType: "text",             
        success: function(response){ 
                var data = jQuery.parseJSON(response);
                datatable = data;
                generateTable(data);                  
                bindTr();
            },
        error: function(xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }
    });
});

AkceptAPI.php(请求)

<?php
//include db configuration file
include_once("connect.php");

//MySQLi query
$results = $mysqli->query("SELECT * FROM oczekujace");
//get all records from add_delete_record table
$array = array();
$tempArray = array();
while($row = $results->mysql_fetch_row())
{
    $tempArray = $row;
 // $array[]= array("ID"=>$row[0],"Nazwa"=>$row[1],"Autor"=>$row[2],"Cena"=>$row[3],"Opis"=>$row[4],"JSON_DATA"=>$row[5]);
    array_push($array,$tempArray)
}

echo json_encode($array);
//close db connection
$mysqli->close();
?>

connect.php

<?php

$username = "root"; //mysql username
$password = ""; //mysql password
$hostname = "localhost"; //hostname
$databasename = 'obrazypol'; //databasename

//connect to database
$mysqli = new mysqli($hostname, $username, $password, $databasename);

?>

【问题讨论】:

  • while($row = $results-&gt;mysql_fetch_row()) ?混合mysql?

标签: php html mysql ajax mysqli


【解决方案1】:

存在语法错误。 array_push 函数后缺少分号。

$mysqli = new mysqli($hostname, $username, $password, $databasename);
//MySQLi query
$results = $mysqli->query("SELECT * FROM category");
//get all records from add_delete_record table
$array = array();
$tempArray = array();
while($row = $results->fetch_row())
{
   $tempArray = $row;
    // $array[]=       array("ID"=>$row[0],"Nazwa"=>$row[1],"Autor"=>$row[2],"Cena"=>$row[3],"Opis"=>$row[4],"JSON_DATA"=>$row[5]);
   array_push($array,$tempArray);
}

echo json_encode($array);
//close db connection
$mysqli->close();

【讨论】:

    【解决方案2】:

    使用

    while($row = $results->fetch_row())
    

    而不是

    while($row = $results->mysql_fetch_row())
    

    【讨论】:

    • 解析错误:语法错误,D:\xampp\htdocs\picturesoflight\akcept\akceptAPI.php 中的意外 '}' 在第 14 行
      现在出现此错误,我看不出有什么问题 oO
    • &lt;?php //include db configuration file include_once("connect.php"); //MySQLi query $results = $mysqli-&gt;query("SELECT * FROM oczekujace"); //get all records from add_delete_record table $array = array(); $tempArray = array(); while($row = $results-&gt;fetch_row()) { $tempArray = $row; array_push($array,$tempArray) } echo json_encode($array); //close db connection $mysqli-&gt;close(); ?&gt;
    • 使用;在这一行之后 array_push($array,$tempArray)
    • @Sko:如果您切换到 IDE(例如 NetBeans、Eclipse、PHP Storm),您的编辑器中会为您突出显示此类错误。
    • 好的,现在正在工作。那是愚蠢的错误......谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多