我想做简单的事情,从 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();
?>
连接.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);
?>
最佳答案
语法错误。 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();
关于php - Ajax MySQL "Unexpected token <",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28450620/