How to call a stored procedure in CodeIgniter USING SQL SERVER - php

I'm bulding my application using Codeigniter and SQL server as my database (I am using the SQLSRV PHP extension to connect to SQL ). The problem happens when I try to call Stored Procedures:
$query = $this->db->query(
"EXECUTE verificacion_fechas '".$codigo."',".$estado.",'".$llave_maestra."','".$fecha_actual."'");
Another way I have tried to create the query with less data is the following , however, it is generating an error:
Error Number: 01000 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Ejecutando SQL directamente, sin cursor. EXECUTE provando15 34,2,'key05','2015-07-22'
I dont really know what im doing wrong. Can someone please help me?

remove the word Execute and it will work.
$query = $this->db->query(
"verificacion_fechas '".$codigo."',".$estado.",'".$llave_maestra."','".$fecha_actual."'");

Here's a sample on how to call a MYSQL STORED PROCEDURE in codeigniter
$this->db->query('CALL procedure_name());

Here's my solution:
first way(if you do not use a variable);
$query=$this->MSSQL->query("EXEC ProcedureName");
second way(if you use a variable);
$query =$this->MSSQL->query("EXEC ProcedureName '$var1','$var2','$var3' ");
or
$query =$this->MSSQL->query("EXEC ProcedureName #varName1='$var1',#varName2='$var2',#varName3='$var3' ");
Note:if you get result like empty array add SET NOCOUNT ON to after BEGIN in your procedure.

Related

Unable to create trigger in PHP for mySQL

I am trying to create a trigger into mySQL using PHP
mysqli_select_db($host, "game");
$sql = "CREATE TRIGGER test_seven BEFORE INSERT ON test FOR EACH ROW INSERT INTO test (name) VALUES ('test')";
$query = $host->prepare($sql);
$query->execute();
And it is not working even though I tried running it on phpMyAdmin and it worked.
EDIT: "This command is not supported in the prepared statement protocol yet"
Is there a way of skipping the prepare statement then ?
Apparently the real problem was the fact that I was not supposed to use the PDO::prepare statement, if anyone else is having this issue use PDO::exec.

Invalid parameter number, SQL state S1093 php odbc mssql

We have a uni student doing work experience at the moment and he's doing a Wordpress prototype for us (as we have neither Wordpress or PHP experience).
It's running on a Windows server and while Wordpress itself is running in mySQL, as all our existing databases are in MS SQL Server 2005/2008 and he's trying to call a stored procedure in a php page using this code:
$connection = odbc_connect('DB', 'UNAME', 'PWORD');
$request = odbc_prepare($connection, "CALL ProcName(?, ?, ?)");
if(!$request) die("Could not prepare statement:" . odbc_errormsg());
$result = odbc_execute($request, array("var1", "var2", "var3"));
if(!$result) die("Could not execute statement:" . odbc_errormsg());
The stored procedure is like this:
ROCEDURE [dbo].[ProcName]
(#option1 varchar(50),
#option2 varchar(50),
#option3 varchar(50))
AS
... lots of logic end with...
select * from tblName
The stored procedure is used both by .net pages and Livelink CMS pages and works correctly but when we try to call it from php, it errors with:
"odbc_execute(): SQL error: [Microsoft][ODBC SQL Server Driver]Invalid parameter number, SQL state S1093 in SQLDescribeParameter in C:\inetpub\wordpress\test.php on line 29"
Strangely, if we rename the procedure call to a non existant stored procedure, it errors with exactly the same thing rather than a stored procedure cannot be found type of error.
We can run sql directly i.e. "select * from etc" and it will return data but we can't call stored procedures (which we use for everything of course!).
Any idea where he could be going wrong?
As I can see from the error message, the error should come from the ODBC driver, because from it's perspective the parameter number differs from prepare to execute statements.
Ask your student to try this approach:
$parms=array("var1", "var2", "var3");
if (!odbc_execute($request, &$parms)) die("odbc_execute failed");
I got the same error when I was doing an native SQL query from Java EE/Hibernate. I was querying "SELECT Count (1) From Table" and I made the mistake of including a mapping class as a parameter. COUNT(1) returns an integer, so it doesn't need to be mapped. Hope that helps.

Syntax error or access violation, SQL state 37000. MSSQL though ODBC from PHP

I'm trying to execute a stored procedure in a ms sql database using the following php:
$query = "{CALL dbo.storedProc('functionName', $date, 'id";
$resultSet = odbc_prepare($connection, $query);
odbc_execute($resultSet, array());
odbc_result_all($resultSet);
The same stored procedure works fine for a different function, and the results are selected by date like so ($date is #Searchstr):
(EventStart >= #Searchstr AND EventStart < DATEADD(DD,1,#Searchstr))
However, when I run the code, it errors without giving any specific hints as to what's causing the error. When the query is run in management studio, the results are returned correctly.
SQL error: [Microsoft][ODBC SQL Server Driver]Syntax error or access violation, SQL state 37000 in SQLPrepare in ...
What's causing the query to error? Could it be an ODBC bug?
Thanks in advance,
Will
The query seems to be missing ')} at the end.

Simple query and error on Codeigniter

I have a problem with CodeIgniter and Mysql. I am getting an error with a very simple query:
$o = "INSERT INTO usuarios (user, password) VALUES ('deesggsd', 'dsggd')";
$query = $this->db->query($o);
$this->db->query($query);
produces:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
1
Filename: C:\wamp\www\newWeb\system\database\DB_driver.php
Line Number: 330
But the query is actually executed; the row appears on the database. What i'm doind wrong?
If I execute the same query on phpmyadmin, all is ok.
Thanks!!!
What you are doing with $query = $this->db->query($o); is executing the query and storing the result to the $query variable. So you've already run the INSERT once which is why it stores properly to the database.
Now when you try to run $this->db->query($query); you're basically trying to run a mysql procedure using the result (TRUE) as your query string. This is where it throws the error. Make sense?
Try doing this instead:
$this->db->insert('usuarios', array(
'user' => 'deesggsd',
'password' => 'dsggd'
);
I suggest looking into Active Record and how interaction between PHP & mysql work in general. No offense but this is a beginner level mistake.
you should use activer recored it's sample and easy,
that will be solve your problem
$values = array('user'=>'deesggsd', 'password'=> 'dsggd');
$this->db->insert('usuarios',$values)
http://ellislab.com/codeigniter/user-guide/database/active_record.html

mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource

I am getting the above error using PHP trying to update an MS SQL server. Any idea what may be happening here? I am using a stored procedure as the basis of the update. I can successfully execute the sproc against the SQL server away from the PHP application.
Any advice/help would be appreciated.
mssql_fetch_array() should be used for SELECT commands, you won't get anything out of UPDATE, INSERT, or DELETE commands.
You can also pass a parameter to the resource by calling mssql_fetch_array($connection) assuming $connection is a valid connection to the DB.
Always test the return value of mssql_query(). If it's ===false mssql_get_last_message() can tell you why the query failed.
$query = 'SELECT x,y,z FROM [foo].[bar].[thingeling]';
$result = #mssql_query($query, $conn);
if(!$result) {
die('MSSQL error: ' . mssql_get_last_message());
}
For debugging purposes you might want to set mssql_min_message_severity and mssql_min_error_severity to more "talkative" values.

Categories