Closed daebak74 closed 5 years ago
Well, that is a lot of code, and it doesn’t seem specific to this framework. But let’s see …
What’s your exact problem here? Is it that you get
{"draw":1,"recordsTotal":1,"recordsFiltered":1,"data":[{"id":"5"}]}
with the (old) test file while you get
SyntaxError JSON.parse unexpected end of data at line 1
with this framework?
Can you just check the target URL of your AJAX request in your browser and see what the actual JSON output is? Can you also open the developer tools of your browser, switch to the “Network” tab and reload the page? What MIME type does it report for the response?
If I get the error from my class I didn't wrote here. The first example is the class out of the framework but I loaded the libraries foundation. Thee result it's fine and return the Json.
If I load the same class in controller with the framework I receive the error.
Yes of coarse I checked with the firebug networks and the content type is correct JSON
But the problem I don't get any even if from controller I try to make a dump nothing. How I can debug for check what is the problem and than I really don't understand why.If in the file work should work inside the controller too.
Thanks
There really isn’t any reason why this should not work in the controller. So I guess the cause for this problem must be quite simple, we just have to find it.
First, please don’t rely on the AJAX request suceeding or any JavaScript logic to work correctly. Just go directly to the URL your AJAX request is trying to fetch and inspect its contents. You say you have already checked the content type for that URL and it’s JSON as expected. Now what about the response body? Is the visible output really the JSON you expect?
Perhaps there is unexpected output before or after the JSON, e.g. error messages or warnings. Or the JSON output is truncated for some reason and the response is thus not complete.
Thanks for the supporting I really tried all. The problem is that the page is totaly empty. The error is in console for the json_encode because the json is empty.
I am trying to recovert everything with your $app->db() for simplified the code but I didn't study it well. By the way If you copy and past the class and test.php and ssp.class.php you can see that work. Maybe give conflict my db connction with the connection of the framework ?
Before I working with mysqli but now that I am thinking PDO support only one instance ?
Anyway don't worry I rewrite the intere class using your $app->db method
Ok I rewrote the entire class and in controller work I tried with a simple select but how I can convert the bind paramenter (sql_exec function)
this is the main function
// Main query to actually get the data
$data = self::sql_exec($app, $bindings,
"SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."`
FROM `$table`
$where
$order
$limit"
);
the qery is something like this:
SELECT SQL_CALC_FOUND_ROWS
idFROM
usersORDER BY
idASC LIMIT 0, 10
and this is the function
static function sql_exec ( $app, $bindings, $sql=null )
{
// Argument shifting
if ( $sql === null ) {
$sql = $bindings;
}
$stmt = $db->prepare( $sql );
// Bind parameters
if ( is_array( $bindings ) ) {
for ( $i=0, $ien=count($bindings) ; $i<$ien ; $i++ ) {
$binding = $bindings[$i];
$stmt->bindValue( $binding['key'], $binding['val'], $binding['type'] );
}
}
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
self::fatal( "An SQL error occurred: ".$e->getMessage() );
}
// Return all
return $stmt->fetchAll();
}
Thanks
The database component has detailed documentation here and allows for the execution of arbitrary statements as well.
But just for this specific problem, it shouldn’t matter if you are using your existing database component or the one from this framework.
I still haven’t seen that you have checked the URL that is fetched via AJAX and its textual content. Do you have APP_DEBUG=1
in your configuration? Then you should see errors or warnings on that page that will help you.
Either the script outputs the correct content or not. If not, there must be an error or problem somewhere that can be debugged.
Debug is enabled. The class work if I just put Array() return the json empty. The new PDO produce in console the error" ....unespected END OF DATA AT LINE 1 COLUMN 1 O THE JSON"
the page is the same /admin/users for GET and POST this is the rooute
$app->get('/admin/users', ['\App\Controllers\Backend\Core\Users', 'getUsers']);
$app->post('/admin/users', ['\App\Controllers\Backend\Core\Users', 'postUsers']);
this is the controller
namespace App\Controllers\Backend\Core;
use Delight\Foundation\App;
class Users {
public static function getUsers(App $app) {
echo $app->view('/backend/'. $_SESSION['BE_THEME'] .'/core/users.html.twig', []);
}
public static function postUsers(App $app) {
require('./app/controllers/backend/core/ssp.class.php');
$app->setContentType('json');
// SQL server connection information
$sql_details = array(
'user' => 'USER',
'pass' => 'PASS',
'db' => 'DB',
'host' => 'HOST'
);
$d = SSP::simple( $sql_details);
echo(json_encode($d));
}
}
this is the class
namespace App\Controllers\Backend\Core;
use Delight\Foundation\App;
class SSP {
static function simple ($sql_details)
{
return array(); // IF I RETURN ONLY THE ARRAY WORK
// THIS GIVE THE ERROR
$db = @new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
$sql_details['user'],
$sql_details['pass'],
array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
);
return array();
}
}
As far as I can see, both POST test.php
and POST /admin/users
return a JSON response and should in fact even have an identical response, right?
If you only change the URL that your AJAX request goes to and then that syntax error appears, your POST /admin/users
is definitely not returning the response you’re expecting. That’s all one needs to know. No need to look anywhere else for the problem. Why don’t you finally inspect and compare the two responses (both header and body)?
Alternatively, if you’re using jQuery, you could change the data type to an explicit dataType: "text"
and then immediately alert
or log (or catch in the debugger) what has been fetched.
Yes I got it. Doesn't return anything but of coarse and sorry. In old my script I had a custom error handler so if prepend (@) in front of any function in this case new PDO the error will be ignored and store in a variable.
I removed it and return this error:
Whoops\Exception\ErrorException: Class 'App\Controllers\Backend\Core\PDO' not found in file ... ssp.class.php on line 38
FIXED ADDING BEFORE SPP CLASS "use PDO;"
namespace App\Controllers\Backend\Core; use Delight\Foundation\App; use PDO; <-- THIS
Thank you for your patience and professional support but out any error message I was becoming crazy.
I tried to use the same instance of PDO and I use
$db = \Delight\Db\PdoDatabase::fromPdo($pdo);
but receive and error
Whoops\Exception\ErrorException: Undefined variable: pdo
By they way I checked the documentation for db but I didn't find any example with Execute a prepared statement with named placeholders and NOT with with question mark placeholders. But I make other test and if I open a question in db repository.
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindValue(':calories', $calories, PDO::PARAM_INT);
$sth->bindValue(':colour', $colour, PDO::PARAM_STR);
$sth->execute();
?>
Thanks.
Class 'App\Controllers\Backend\Core\PDO' not found
Glad you found it. See, that’s why I suggested really checking the output and especially looking for any warnings or errors. That one here is because the file is in a namespace, so if a class has no namespace specified and is not imported explicitly, like PDO
here, it looks for that class in the current namespace, where it might not exist.
use PDO;
Alternatively, you could just change PDO
to \PDO
so that it is known it is from the root/global namespace.
Undefined variable: pdo
That error message seems pretty clear: The variable does not exist at that point.
By they way I checked the documentation for db but I didn't find any example with Execute a prepared statement with named placeholders and NOT with with question mark placeholders.
Good point, the documentation mostly uses positional placeholders (question marks) instead of named placeholders. But you can use named placeholders in the same way.
Thanks, $pdo variable doesn't exist there this is sure :) $pdo is the current instance correct ? I can get the current instance in function of my class without create a new PDO.
Hi, It's hours now that I try everything but nothing. I hope to find help for this problem.
The problem This class work and I use for populate a table from db. I load the framework too and no problem. Below e test file and the class. When I use in controller I receive a SyntaxError JSON.parse unexpected end of data at line 1 of the JSON data and I can't take any output. :(
I know that I should to create a new DB connection but just for try it
test.php
ssp.class.php
WORK and Result
{"draw":1,"recordsTotal":1,"recordsFiltered":1,"data":[{"id":"5"}]}
Now in production
Controller
[POST by Ajax]
and the class above at the top of the class I added
namespace App\Controllers\Backend\Core;
Where I make mistake and how I can solve
Thanks