Using stored procedures with cakephp 3.x - cakephp

I am trying to use a SQL Server stored procedure with cakephp 3.x, but I get a database error.
This is my controller:
use Cake\Datasource\ConnectionManager;
if($this->request->is('post')){
$data = $this->request->getData();
$param1 = $data['playId'];
$param2 = $data['cardboard'];
if(is_numeric($param1) && is_numeric($param2)){
$param1 = $data['playId'];
$param2 = $data['cardboard'];
$conn = ConnectionManager::get('ssql');
$requested = $conn->query(
'call searchPrem(?, ?)',
[$param1, $param2]
)->fetchAll('assoc');
//$exec = debug($requested);
}
echo json_encode( $requested );
}
I get this SQL error:
Error: SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near '#P1'.
How can I execute a stored procedure and retrieve the result data?

I found the solution:
$conn = ConnectionManager::get('ssql');
$sql = $conn->execute("yourfunction #id = $playId, #type = $type")->fetchAll('assoc');

In Cake 2.x I used solution like below:
if ($connection = $this->getDataSource()->connection) {
$statement = oci_parse($connection, "DECLARE x number; BEGIN :x := searchPrem(" . $var1 . ", " . $var2 . "); END;");
oci_bind_by_name($statement, ':x', $result, 10);
if (oci_execute($statement)) {
// Success
}
}
This example works with Oracle dialect. You should modify to Cake 3.x and Your database dialect.

Related

How can I invoke sp_prepexecrpc in SQL Server?

I'm currently trying to invoke in SQL Server through ODBC. I tried executing queries like:
Declare #Out int;
EXEC #Out output,
N'#myNumber int',
N'EXEC OdbcOneParameterProcedure #myNumber',
#myNumber = 0;
EXEC #Out;
and
DECLARE #Out int;
EXEC #Out OUTPUT,
N'OdbcOneParameterProcedure',
#myNumber = 0;
SELECT #Out
But all I see is it was invoked in SQL Server as SQL Batch in packet trace in Wireshark. What else should I try?
I figured it out, one way of solving this is executing your StoredProcedure through PHP connector to ODBC using PDO
$odbcDataSource = 'dataSource';
$user = 'userName';
$password = 'userPassword';
$call = "{CALL OdbcOneParameterProcedure( ? )}";
$connection = new PDO($odbcDataSource, $user, $password);
if (!$connection) {
echo 'Connection failed' . odbc_errormsg();
} else {
echo 'Connected';
$param = 0;
$statement = $connection->prepare($call);
$statement->bindParam(1, $param, PDO::PARAM_INT);
$statement->execute();
// dump result to quickly verify correctness of query
$result = $statement->fetchAll();
var_dump($result);
}
$connection = null;

Database connection script crashes after 4 successful connections

I need to query a group of SAP sybase databases for some information and print that as a comma spearated list. So I figure I write a perl script that connects to any of those databases via DBI module. Here is what I came up with.
my $user = "someuser";
my $passwd = "somepassword";
my #sids=(filled with DB identifiers);
my $output="";
my $size;
my $version;
my $id;
my $dsn;
my $dbh;
my $sid;
my #row;
my $sth1;
my $sth2;
foreach $sid (#sids) {
print $sid."\n";
$dsn = "dbi:Sybase:server=$sid;charset=iso_1;tdsLevel=CS_TDS_50";
print $dsn."\n";
$dbh = DBI->connect($dsn, $user, $passwd,{ PrintError => 0,RaiseError => 0, AutoCommit => 1, syb_enable_utf8 => 1});
print "DBI OK\n" if defined ($dbh);
$sth1 = $dbh->prepare('select SUM(size) from master..sysusages WHERE dbid = 4 AND segmap = 3');
$sth2 = $dbh->prepare('select ##version');
$sth1->execute;
while (#row = $sth1->fetchrow) {
$size = $row[0];
}
$size = $size * 16 / 1024;
$sth1->finish;
$sth2->execute;
while (#row = $sth2->fetchrow) {
$version = $row[0];
}
$sth2->finish;
$output = $sid.",".$size.",".$version;
$dbh->disconnect;
print $output."\n";
}
When I execute this, it crashes after 4th iteration, because the connection handle is not set. So the connection of the fifth DB does not work anymore.
Can't call method "prepare" on an undefined value at ./check_sybasedbs.pl line 36.
Line 36 is the preparation of statement 1.
I tried putting sleep commands at various positions. I also tried to explicitly clean up the variables that are reused via undef. Now I am out of ideas and would really appreciate your input.
Your code could be written as sample below (please see if ... else ... block for $dbh)
use strict;
use warnings;
use feature 'say';
use DBI;
my($user, $passwd) = qw/someuser somepassword/;
my #sids = qw/server1 server2 ... server#/;
foreach my $sid (#sids) {
my $dsn = "dbi:Sybase:server=$sid;charset=iso_1;tdsLevel=CS_TDS_50";
say "DSN: $dsn";
my $dbh = DBI->connect($dsn, $user, $passwd, { PrintError => 1,
RaiseError => 1,
AutoCommit => 1,
syb_enable_utf8 => 1
}
);
if( not defined ($dbh) ) {
say "WARNING: Could not connect to $dsn";
} else {
say "INFO: DB connection established";
my($size,$version);
my $query = 'SELECT
SUM(size)
FROM
master..sysusages
WHERE
dbid = 4
AND
segmap = 3
';
my $sth = $dbh->prepare($query);
$sth->execute;
while (#row = $sth->fetchrow) {
$size = $row[0];
}
$sth->finish;
$query = 'select ##version';
$sth = $dbh->prepare($query);
$sth->execute;
while (#row = $sth->fetchrow) {
$version = $row[0];
}
$sth->finish;
$dbh->disconnect;
$size = $size * 16 / 1024;
say "SID: $sid, SIZE: $size, VERSION: $version";
}
}
NOTE: use strict; use warnings; helps to avoid many pitfalls, use diagnostics; helps to identify a problem in difficult cases
NOTE: $sth->fetchrow_hashref allows address hash element by name, no need to count index of array as in case $sth->fetch_rowarray
in my naiv thinking I hid some lines of code that I was convinced could not be the reason for this misbehaviour. As it turns out, it was. So the reason for my problem was a simple logical error, that caused the password, that was used after a connect to a certain DB to be wrong.

Perl DBI SQL SERVER CONNECTIVITY ISSUES

Very new to Perl. I am having issues trying to get DBI to communicate to a SQL server 2008 DB.
I get the following error when I try and connect to SQL Servereveb when I try to use ODBC or directly.
I am new to Perl, can someone please assist...thanks
install_driver(MSSQL) failed: Can't locate object method "set_sql" via package "Class::DBI::MSSQL" at C:/Perl/lib/DBD/MSSQL.pm line 79. Compilation failed in require at (eval 19)C:/Perl/site/lib/DBI.pm:744 line 3.
use strict;
use warnings;
use diagnostics;
use Class::DBI::Loader;
use DBI;
use File::Glob ':glob';
my $DBUserName = "*******";
my $DBPassword = "*******";
my $DBName = "dbi:MSSQL:uat-dbserver1";
my $dbh = "";
my $sqlStatement = "";
my $sqlCmd = "";
my #EasySetTableNames = ();
$dbh = DBI->connect( $DBName, $DBUserName, $DBPassword,
{ PrintError => 0, AutoCommit => 0})
|| die "Database connection creation failed: $DBI::errstr\n";
$sqlStatement = "SELECT * from tableA ";
$sqlCmd = $dbh->prepare($sqlStatement);
$sqlCmd->execute();
#EasySetTableNames = #{$dbh->selectcol_arrayref($sqlStatement)};
print "hi";
and via ODBC
#!/usr/bin/perl -w
use strict;
use DBI;
# Replace datasource_name with the name of your data source.
# Replace database_username and database_password
# with the SQL Server database username and password.
my $data_source = "dbi:MSSQL:test";
my $user = "test";
my $password = "test";
# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect($data_source, $user, $password)
or die "Can't connect to $data_source: $DBI::errstr";
# This query generates a result set with one record in it.
my $sql = "SELECT 1 AS test_col";
# Prepare the statement.
my $sth = $dbh->prepare($sql)
or die "Can't prepare statement: $DBI::errstr";
# Execute the statement.
$sth->execute();
# Print the column name.
print "$sth->{NAME}->[0]\n";
# Fetch and display the result set value.
while ( my #row = $sth->fetchrow_array ) {
print "#row\n";
}
# Disconnect the database from the database handle.
$dbh->disconnect;
Any help you can provide would be so appreciated.
I usually use the ODBC driver from within the dbi and this is how I would usually hit sql server (2008 r2)
#!/export/appl/pkgs/perl/5.8.4-fiq/bin/perl
#!/usr/bin/perl
#!/bin/sh
use strict;
use DBI;
use Time::localtime;
use Data::Dumper;
my $dsn = 'DBI:ODBC:Driver={SQL Server}';
my $host = 'xxx\yyy';
my $database = 'testing';
my $user = 'user';
my $auth = 'password';
my $dbh = DBI->connect("$dsn;Server=$host;Database=$database", $user, $auth) or die "Database connection not made: $DBI::errstr";
my $sql = "EXECUTE database.schema.sproc";
my $stmt = $dbh->prepare($sql);
$stmt->execute();
$stmt->finish();
$dbh->disconnect;
I was able to connect to SQL Server using OLE32, here is an example of the code..."Cursor type changed" error on Perl OLE32 MSSQL dateadd function results

How to fetch and display an array using PDO?

I understand there are MANY ways to do all of this, but trying to do it the best way.
I have created the db parameters, dns, dbh, sth, sql and generally quite happy with the result up to ... well ... the result part.
<?php
// db parameters
$dbhost = "localhost";
$dbname = "x";
$dbuser = "y";
$dbpass = "z";
// driver invocation (dsn is short for data source name)
$dsn = "mysql:host=$dbhost;dbname=$dbname";
// create db object (dbh is short for database handle)
$dbh = new PDO($dsn, $dbuser, $dbpass);
// execution of database query (sth is short for statement handle)
$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();
NOT SURE WHAT TO PUT BELOW.... (A) or (B)
I just want to present a simple array of the data. One row from the table per line.
Option A
echo $_POST['fieldname1'];
echo $_POST['fieldname2'];
echo $_POST['fieldname3'];
Option B
while ($rows = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $row[fieldname1],'<br>';
}
AND I AM CONFIDENT WITH THE ENDING
$dbh = NULL;
?>
Any advise would be GREATLY appreciated.
UPDATED CODE: (Produces nothing on the page)
<?php
// db parameters
$dbhost = "localhost";
$dbname = "theaudit_db1";
$dbuser = "theaudit_user";
$dbpass = "audit1999";
$dsn = "mysql:host=$dbhost;dbname=$dbname"; // driver invocation (dsn is short for Data Source Name)
try {
$dbh = new PDO($dsn, $dbuser, $dbpass); // connect to new db object (dbh is short for Database Handle)
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to enable exceptions
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // set the PDO emulate prepares to false
// execute query to database (sth is short for Statement Handle)
$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$dbh = NULL;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Though I can't get what's the connection between A anb B, I can answer the
I just want to present a simple array of the data. One row from the table per line.
question.
$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
where $data is a sought-for array.
The problem with your updated code is simple - you arent echo'ing your data out. You need to add something like..
foreach($data as $arKey=>$dataRow){
foreach($dataRow as $arKey=>$rowField){
echo $rowField.','; //concat with a ',' to give csv like output
}
echo '<br>'; //to get to next line for each row (may want to trim the last ','
}
I am also confused by the reference to $_POST. It is true both are associate arrays but that does not mean that the $_POST option is viable - the data would only be available in the $_POST if you put it there (eg $_POST = $data) which would be pointless. Or if you had posted the data from somewhere else. Neither seem to fit what you are asking so I would forget about the $_POST and just figure out how you access your multi dimensional $data array. There is endless tut's on this subject. Try using
var_dump($data)
to see whats inside that should help you visualise what is going on.
NOTE: in option B you are not correctly concatenating or referencing your array it should be:
while ($rows = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $rows[fieldname1].'<br>'; //$row doesnt exist its $rows and you use . to concat not ,
}
Ah yes and probably better to use unset rather than setting $dbh to equal null
unset($dbh);

Running a PL/SQL procedure in a Perl script

I have a Perl script which takes a file as input and has PL/SQL (for statements and DBMS_OUTPUT.PUT_LINE) in it. I need to run make a database connection and run that file in Perl script.The pl/sql has Begin declare end section,with for statements on it which is writing data of 3 columns separated using commas(DBMS_OUTPUT.PUT_LINE) I have shown what I have tried below. Is it correct?
my $dbh = DBI->connect( "dbi:Oracle:$db", $username, $passwd ) ||
die( $DBI::errstr . "\n" );
$dbh->{AutoCommit} = 0;
$dbh->{PrintError} = 1;
open FILE, $sql_file or die "Could not open file";
$query = '';
while($line = <FILE>) {
chomp($line);
$query .= $line;
}
my $sth = $dbh->prepare($query);
$sth->execute();
while ( my #row = $sth->fetchrow_array() ) {
foreach (#row) {
print "$_";
}
print "\n";
}
$sth->fetch(), $sth->fetchrow_*(), and friends all fetch records from a result set. In Oracle PL/SQL blocks don't normally return result sets. So calling $sth->fetchrow_array() after running a PL/SQL block won't return any results.
If your using DBMS_OUTPUT.PUT_LINE to output results you will need to use the dbms_output_* functions provided by DBD::Oracle.
my $dbms_output_byte_limit = 1000000;
$dbh->func( $dbms_output_byte_limit, 'dbms_output_enable' );
my $sth = $dbh->prepare($query);
$sth->execute();
my #text = $dbh->func( 'dbms_output_get' );

Resources