Hello SC group,
I have some code that I need to use in server applications. I use SC macros through this code.
Is it possible to put this code in a library and call it from there? I have tried it but I’m getting undefined function errors so I’m thinking I will need to convert it to pure PHP?
Thanks for your advise.
Which macros are you talking about?
sc_lookup and sc_select for 2
thanks for taking the interest in my question.
I have some libraries using those macros and they work just fine.
But depending on where you’re calling the lib, it won’t work. For example:
I’ve just created a new lib that uses sc_lookup and called it inside onApplicationInit event and it didn’t work (the error message is: Call to a member function Execute() on a non-object). Then I removed it from onApplicationInit and put inside onScriptInit and it worked.
Maybe that’s the same case as yours. Have a look at the macros documentation to see where you can use those macros…
Mamede, I will do just that.
Thank you for your assistance.
You’re welcome!
Well I am still not having any success getting SC Marcos IE sc_select or sc_lookup to execute from within a library.
Any help is greatly appreciated, I have been struggling with this for such a long time. Thanks
I am running this calling code from a blank/execute (I have also tried a form/OnScriptInit)
sc_include_library(“prj”,“PhaseStepState”,“test.php”,true,true);
meTest();
The code in the library is below and produces this error: Fatal error: Call to undefined function sc_select() in /home/bwwalker/public_html/scriptcase/app/dr2/_lib/libraries/grp/PhaseStepState/test.php on line 13
If I remove the comment block on the next line I get this error:
Parse error: syntax error, unexpected ‘{’ in /home/bwwalker/public_html/scriptcase/app/dr2/_lib/libraries/grp/PhaseStepState/test.php on line 10
<?php
function meTest(){
echo “In meTest function”;
$sql = "SELECT
PropId
FROM Properties ";
sc_select(my_data, $sql);
/*
if ({my_data} === false)
{
echo “Access error. Message =”. {my_data_erro};
}
else
{
$cnt = 0;
while (!$my_data->EOF)
{
$cnt++;
$propId = $my_data->fields[0];
$my_data->MoveNext();
}
$my_data->Close();
}
echo "record cnt = ".$cnt;
*/
}
?>
<head/>
I am going to try to show/explain this again using a couple of screenshots. A picture is worth a thousand words right?
Screenshot 1 shows the code in the calling app which is a blank. I have it executing the exact same simple code that is also in the library just to show that the code works. Then it calls the library function to run the same code.
Screenshot 2 shows the library code and the lines that the errors are produced on and the errors produces.
Thanks for any insight you might be able to offer.
Bill
I assumed you were talking about the “Internal Library” option, my fault.
As far as I know, there’s no way to use macro sc_select in an external library, as it’s not created internally in the application’s class. When you deploy your application, you can see that the external library is created in a separated file and ScriptCase doesn’t convert its code, leaving it exactly as you wrote it. And that’s the reason it can’t find the sc_select macro/function.
Original ‘test1’ (internal library):
<?php
function test1(){
$sql = "SELECT * FROM tb_categories";
sc_select(rs, $sql);
var_dump($rs);
}
?>
After deploy (its code is copied to the application’s class and converted to this):
function teste1(){
$_SESSION['scriptcase']['___blank']['contr_erro'] = 'on';
$sql = "SELECT * FROM tb_categories";
$nm_select = $sql;
$_SESSION['scriptcase']['sc_sql_ult_comando'] = $nm_select;
$_SESSION['scriptcase']['sc_sql_ult_conexao'] = '';
if ($rs = $this->Db->Execute($nm_select)) {
}elseif (isset($GLOBALS["NM_ERRO_IBASE"]) && $GLOBALS["NM_ERRO_IBASE"] != 1){
$rs = false;
$rs_erro = $this->Db->ErrorMsg();
}
var_dump($rs);
$_SESSION['scriptcase']['___blank']['contr_erro'] = 'off';
}
Original ‘test2’ (external library):
<?php
function test2(){
$sql = "SELECT * FROM tb_categories";
sc_select(rs, $sql);
var_dump($rs);
}
?>
After deploy (its code is in a separated file and is exactly the same code; besides that, there is no function named ‘sc_select’):
<?php
function test2(){
$sql = "SELECT * FROM tb_categories";
sc_select(rs, $sql);
var_dump($rs);
}
?>
Mamede, Thanks for your reply. So if I understand you correctly I can create an Internal Library and copy my same code to it and it should work? I believe I will need to use the different ‘include’ statement for internal Libraries.
Oh, by the way I love your ‘Scriptcase Tools’ thanks so much for doing that for us.
Yes, it should work.
There’s a workaround, where you pass the variable that references the application’s class/object ($this) to your function and then you can execute a SQL statement just like the macro does (but there’s no guarantee it’s going to work forever, as Netmake can - despite being unlikely - change the way to execute a SQL statement). Something like this:
In an external library:
<?php
// $app should be your application's class/object, so yourFunction can access the Db->Execute function
function yourFunction($app){
$sql = "SELECT * FROM tb_categories";
$my_data = $app->Db->Execute($sql);
print_r($my_data->fields);
}
?>
Now you can call yourFunction from a blank application (the variable $this is where all application settings/options are stored, including the database object):
<?php
yourFunction($this);
?>
I’m glad you enjoyed it!
I’d like to add more functionalities to it, so if you have any suggestion, please send it to me :rolleyes:
Hi Mamede
I tried to read your blog using Google Translate.
Unfortunately the Menu gets in the way…
If you can make your blog usable via Google Translate I would enjoy reading it.
[ATTACH=CONFIG]n71048[/ATTACH]
Sorry for taking so long to answer you!
Actually I want to translate all my blog to English, but English is not my mother language so it takes some time :rolleyes: :rolleyes: :rolleyes:
I’ve just translated some of the articles (only 2 left now), so you can already read them ^^
Thanks!!
Great, thanks!
Actually the English in your articles is excellent.
It’s very nice of you to say that!
I’m still learning and it’s been fun so far!