It is necessary to connect to the potgeSQL database and output the results in JSON format. I do this:

<?php header('content-type: application/json; charset=utf-8'); $success = 'true'; $message = "Загруженные данные"; $connectString = 'host=localhost dbname=stocks_db user=postgres password=95161617'; $link = pg_connect($connectString); if (!$link) { $success = 'false'; $message = "Ошибка соединения"; } else { $sql = "select *, open - last as change from stocks"; if( !$result = pg_query($link, $sql) ) { $success = 'false'; $message = "Ошибка выполнения запроса"; }; $rows = array(); while($r = pg_fetch_assoc($result)) { switch ($r['split']) { case 't': $r['split']=true; break; case 'f': $r['split']=false; break; } $rows[] = $r; } } print "{\"success\":$success,\"message\":\"$message\",\"data\":"; print json_encode($rows); print "}"; ?> 

But as a result - an empty tab in the browser !!

What is a mistake in me? How to fix it ???

  • You have two functions php.net/manual/ru/function.pg-er-error.php and also php.net/manual/ru/function.pg-result-error.php and you ask us what is your mistake? .......... in which IDE do you develop? In a notebook? After all, even the stackoverflow editor shows you that there are no quotes after the Ошибка выполнения запроса and any decent editor shows it all the more - Alex Shimansky
  • @ Alexey Shimansky, I apologize .. In phpStorm I have quotes all, apparently when copying it happened! - Maybe_V
  • OK. nevertheless, I showed you at least two functions with the help of which you can find out what errors you have .... moreover, you can enable the display of errors in php so that it can also show your errors .. ini_set('error_reporting', E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('error_reporting', E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); - Alexey Shimansky
  • Such a query select *, open - last as change from stocks and conceived? spaces in open - last ... and is there such a column? - Alexey Shimansky
  • @ Alexey Shimansky, throws such an error Fatal error: Call to undefined function pg_connect () in /var/www/mysite/lab2/stocks.php on line 17 - Maybe_V

0