PHP/MSSQL/SQL Profiler: not executing incoming SQL - php

I have a very simple INSERT statement that I can run using SSMS. When I run the same INSERT statement using PHP mssql_execute or mssql_query SQL profiler shows me that the 'TextData' is identical when sent in by PHP as it is when sent in by SSMS. The problem is when executed via the PHP code, the INSERT never actually happens, whereas when run from within SSMS, it inserts just fine. Any ideas?
MS SQL: 2008 R2
PHP: 5.4, freetds 8.0
PHP error:
PHP Warning: mssql_execute(): message: Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services. (severity 16) in /home/...
Edit: PHP relevant PHP added
<?php
putenv('FREETDS=/home/###/freetds.conf');
putenv('FREETDSCONF=/home/###/freetds.conf');
$link = mssql_connect('mssqlserver', '###','###'); // connect
if ( !$link ) {
if ( function_exists('error_get_last') ) {
var_dump(error_get_last());
}
die('connection failed');
}
// Create a new statement
$stmt = mssql_init('Db.dbo.SprocName');
// Some values
$a = 1;
$b = 'test';
$c = 'myname';
// Bind values
mssql_bind($stmt,'#a',$a,SQLINT2,false,false);
mssql_bind($stmt,'#b',$b,SQLVARCHAR,false,false,8000);
mssql_bind($stmt,'#c',$c,SQLVARCHAR,false,false,50);
// Execute the statement
mssql_execute($stmt);
// And we can free it like so:
mssql_free_statement($stmt);
?>

The stored procedure (Db.dbo.SprocName) did a simple insert on a table, lets call it (Db.dbo.TableName). When I removed the foreign key from Db.dbo.TableName then the PHP mssql INSERT calls started working.
I realize this technically resolves the issue, but in practice its not acceptable. Those FK's are there for a reason :). Any thoughts?

Related

mssql_connect will not connect different DB on same SQL Server

$conn_161 = "192.168.0.161"; //local serwer adress
$user_161 = "ME";
$pass_161 = "what_is_the_password?";
$connect_161 = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'DUKENUKEN3D' , $connect_161 );
//as requested - 1st DB connection and 1st query
$q_duke = "select * from DUKE /*DB1*/";
$r_duke = mssql_query($q_wartownik,$connect_161); //result
$connect_different_db = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'BIGMAN' , $connect_different_db );
//second db and query
$q_bigman = "select * from BIGPEOPLE /*DB2*/";
$r_bigman = mssql_query($q_bigman,$connect_different_db ); //result
Error:
Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'DUKE'.
Yes I know mssql_select_db is old, but I need to use it in this project. As you see I try to connect to same server but select 2 different DB at the same time, and run queries in the same php page (to connect data from 2 different DB). It seems it is not valiable? I even tried to run mssql_select_db just before doing the query to second DB, and then changing it back to first DB.
I understand this is limitation of the library (I will run all queries from LAST selected DB).
Is there a workaroud? Because all I got is to create page inside invisible iframe and there run php page with different db connection - far from good solution.
I would expect that this will work the same as it would if you were running this in a SQL environment directly (e.g. you can try it in SSMS or from the command line).
You can specify the database name when you reference the table in the query: e.g.
select * from db1.dbo.DUKE
This is standard SQL Server behaviour whenever you want to refer to an object which is outside the context of the current database.

mysqli_commit fails when select statement is added

Problem:
Unable to store data with mySQL stored procedure with mysqli_begin_transaction.
Details:
The below code will do simple insert and select using mysql stored procedure. Code runs fine without select statement. However once the select statement is added, it won't commit any data even the query returns success at PHP side.
Snippets (PHP):
$DB_DRRM_SQLI = mysqli_connect("localhost","root","", "sandbox_db");
mysqli_begin_transaction($DB_DRRM_SQLI);
$SQL_QUERY_CODE = "CALL SANDBOX_TEST()";
$DB_QUERY = mysqli_query($DB_DRRM_SQLI, $SQL_QUERY_CODE);
// ERROR REPORTING
if($DB_QUERY === false)
{
echo mysqli_error($DB_DRRM_SQLI);
mysqli_rollback($DB_DRRM_SQLI);
}
else
{
echo 'success';
mysqli_commit($DB_DRRM_SQLI);
}
exit;
Snippets (mySQL Stored procedure):
BEGIN
INSERT INTO
`sandbox_table`
(
`SOME_STRING`
)
VALUES
(
'ABCDEFGHIJKL...'
);
SELECT
LAST_INSERT_ID() AS INSERTED_ID,
'ABCDE...' AS OTHER_PARAMS;
END
Database (Table sandbox_table):
RECORD_PRIMARY_ID (Int - Auto increment)
SOME_STRING (Varchar - 500 length)
Spec:
PHP version: 5.6.14
10.1.8-MariaDB
Storage Engine: InnoDB
Notes:
If transaction is made at stored procedure works fine, but I need a PHP managed transaction to handle multiple query requests and response depending on the result of query.
(It can be a possible last resort if there's no other solution, where I need to convert whole PHP code to stored procedure and need pass tons of parameter)
Methods Tested:
Tried with other PHP version 7.0.9 with same result (10.1.16-MariaDB)
Tested with new database with no other data except sandbox_tableand above stored procedure.
Tested without additional include libraries (tested with purely on above snippets).
Solution:
It was caused by Commands out of sync error at mysqli_commit. Seems the mysqli won't allow committing transaction while the query is open, which happens if you add select statement to above stored procedure.
So to handle this, it must close the query first or put the query to buffer.
Snippets (PHP):
// SQL Database
$DB_DRRM_SQLI = mysqli_connect("localhost","root","", "sandbox_db");
mysqli_begin_transaction($DB_DRRM_SQLI);
$SQL_QUERY_CODE = "CALL SANDBOX_TEST()";
$DB_QUERY = mysqli_query($DB_DRRM_SQLI, $SQL_QUERY_CODE);
// ERROR REPORTING
if($DB_QUERY === false)
{
echo mysqli_error($DB_DRRM_SQLI);
mysqli_rollback($DB_DRRM_SQLI);
}
else
{
// Must free current query result before committing transaction
#mysqli_free_result($DB_QUERY);
#mysqli_next_result($DB_DRRM_SQLI);
if(mysqli_commit($DB_DRRM_SQLI) === false)
{
echo mysqli_error($DB_DRRM_SQLI);
}
else
{
echo 'success';
}
}
exit;

Using PHP to connect to MS Access DB using ODBC keeps on locking

I know this is not the best setup, but I'm stuck with it and cannot change it.
PHP connects to a local MS Access DB (.mdb) to log certain activities being done by the script. This works fine when only one instance of the script is running. However, if run two instances, I occasionally get the following error:
odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Could not update; currently locked., SQL state S1000 in SQLExecDirect
This only happens intermittently so I'm assuming it's only when the two scripts both happen to be trying to write to the same table at the same time. I never had this issue with other DBs such as MySQL. How can I tell PHP/ODBC/Access to either not lock or try again if it is locked?
I also usually have the Access DB open on my screen for troubleshooting purposes.
odbc_exec() returns FALSE if the query fails, so you could just check the result of the query and keep trying until it succeeds:
$sql = "UPDATE [Clients] SET [Position]='somewhere' WHERE [ID]=1";
while (TRUE) {
$result = odbc_exec($conn, $sql);
if ($result) {
break;
}
else {
$lastError = odbc_error();
if ($lastError != "S1000") {
echo "ODBC error $lastError";
break;
}
}
sleep(1);
}

CodeIgniter - cannot execute procedure with couple UPDATE statements

Have an issue with executing routines in CodeIgniter.
I have a routine that just updates couple mysql tables with the data specified in parameters.
Originally I received:
Error Number: 2014
Commands out of sync; you can’t run this command now
Tried the solution specified at http://codeigniter.com/forums/viewthread/96583/
$this->mydb->Query($sql);
with changed driver to mysqli
It works fine with the select statements for me but if the routine has only couple updates - it does not work.
The problem is that there is no error message is returned. It behaves like it is successfully run, but tables are not updated.
NOTE: there should not be an issue with routine itself since if I print the query in question and try to execute this in MySQL Workbench - tables are updated correctly.
Any advises are appreciated.
Try changing the dbdriver in the config file to mysqli:
$db['default']['dbdriver'] = "mysqli";
$this->mydb->Query used in the whole project for each query has solved the issue
function call( $procedure )
{
$result = #$this->db->conn_id->query( $procedure );
while ( $this->db->conn_id->next_result() )
{
//free each result.
$not_used_result = $this->db->conn_id->use_result();
if ( $not_used_result instanceof mysqli_result )
{
$not_used_result->free();
}
}
return $result;
}

Executing SQL directly; no cursor in PHP when calling a stored procedure

I'm getting the following error when trying to execute a stored procedure from PHP using sqlsrv driver:
Executing SQL directly; no cursor
Add a cursor option to your connection:
$connection = odbc_connect('host','username','password', 1)
or die('Connection failed!');
After password insert cursor_option 1 <-- this option was missing.
I got the same error when I used code that basically did this:
$options = array( "Scrollable"=>SQLSRV_CURSOR_STATIC);
$sql = "exec dbs_webgetnextcount 'expense'";
sqlsrv_query($db,$sql,null,$options);
I fixed it by not setting the cursor.
$sql = "exec dbs_webgetnextcount 'expense'";
sqlsrv_query($db,$sql);
PS. I was doing the CURSOR_STATIC option on my select statements so that sqlsrv_num_rows() would give a correct number.
I had the same problem as Jon but I figured since it is just a warning generated by the sql data access component and the query still completes successfully you can disable it in sqlsrv.exe using sqlsrv_configure("WarningsReturnAsErrors", 0);
Could be a permissions issue:
http://griffo.info/tellme/2009/05/executing-sql-directly-no-cursor/

Categories