Call to undefined function ExecuteScalar()

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mgqz
User
Posts: 100

Call to undefined function ExecuteScalar()

Post by mgqz »

Hello, I was using this code in PHP20 for a stand alone php with some queries to my data but cannot find if there was a change in phpm24 to use executescalar.

thank you in advance for any guidance

<?php
namespace PHPMaker2022\agro_met;

$sql="";
$geojsonstr="";
// Autoload
include_once "autoload.php";
$op=$_GET['op'];

if ($op=='lastold')
{
$sql="SELECT json_build_object('type', 'FeatureCollection', 'features', json_agg(st_asgeojson(t.*)::json)) AS response
   FROM ( SELECT * from api_last) t;"; //outputs geojson
   $geojsonstr =ExecuteScalar($sql);
} //last

// header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header('Content-Type: application/json; charset=utf-8');


echo $geojsonstr;
?>

and it outputs json.

Wanted to do the same in php2024 but gives this error:

Fatal error: Uncaught Error: Call to undefined function PHPMaker2024\agro_met\ExecuteScalar() in C:\xampp_81\htdocs\agro-met\_api.php:175 Stack trace: #0 {main} thrown in C:\xampp_81\htdocs\agro-met\_api.php on line 175

this is the phpm2024 code.

<?php
namespace PHPMaker2024\agro_met;

$sql="";
$geojsonstr="";
// Autoload
include_once "vendor/autoload.php";

$op=$_GET['op'];

if ($op=='lastold')
{
$sql="SELECT json_build_object('type', 'FeatureCollection', 'features', json_agg(st_asgeojson(t.*)::json)) AS response
   FROM ( SELECT * from api_last) t;"; //outputs geojson
   $geojsonstr =ExecuteScalar($sql);
} //last

// header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header('Content-Type: application/json; charset=utf-8');


echo $geojsonstr;
?>

arbei
User
Posts: 9787

Post by arbei »

The autoload.php only loads the composer packages in the vendor folder, it does not load the PHPMaker common files and global functions. Avoid using standalone files. You may read:

  1. Custom Files
  2. Using Route_Action server event
  3. How to Load Data from Database into Custom Files

mgqz
User
Posts: 100

Post by mgqz »

Thank you for your response, as you can see in my code I want to output json so the include common files (header/footer) etc would not apply, will it add the other functions ?

I generate a custom file but if I do not include common files i dont know how to do a simple query based on url parameter and return a json..

I have been able with plain php with pg_query and pg_fetch_assoc but I want to be contained in the project using the project connections and functions.

Any help or guidance is appreciated, as mentioned before with phpm20 I just add the namespace and use ExecuteScalar

Thanks


arbei
User
Posts: 9787

Post by arbei »

You may read above three options and choose one that suits yourself best.


mgqz
User
Posts: 100

Post by mgqz »

Yes. thanks for your response.

I see maybe route_action or api_action .

Does anyone know is there are any training related to PHPM24 other than the documentation.

Best


Post Reply