Reputation: 981
I want to do simply thing, get table from MySQL database. Earlier i had no MySQLi code, simple MySQL and it was working. Now, after change to MySQLi i got error:
Uncaught SyntaxError: Unexpected token <
I have this code:
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 (request)
<?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);
?>
Upvotes: 1
Views: 701
Reputation: 2785
There is syantax error. semicolon missing after array_push function.
$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();
Upvotes: 1
Reputation: 25352
Use
while($row = $results->fetch_row())
instead of
while($row = $results->mysql_fetch_row())
Upvotes: 1