PHP and MSSQL Multiple Database Connections - php

I'm having a weird issue with connecting to the master database twice.
$devdb01 = $this->load->database('master',true);
$dbstate = $this->load->database('mastercheck',true);
I have 2 different connection id's but it seems if i'm running a query on the master database it won't execute another query even though its a separate database connection. Basically i'm trying to use a SP that runs a restore database and then run another query checking if the database is still in a restoring state.
I run the queries like the following:
$dbresult=$devdb01->query($query);
$dbcheck=$dbstate->query($checkquery);
echo $devdb01->conn_id;
echo $dbstate->conn_id;
$query = "EXECUTE master..sqlbackup '-SQL \"RESTORE DATABASE $dbname FROM DISK = [$restoredir$backupselected] WITH REPLACE, MOVE [$dataname] TO [$datadir$dbname$dataprepend] , MOVE [$logname] TO [$logdir$dbname$logprepend]\"'";
$dbresult=$devdb01->query($query);
$checkquery="SELECT state_desc from sys.databases where [name]='$dbname'";
$dbcheck=$dbstate->query($checkquery);
var_dump($dbcheck);
I just get an output of bool(false) for that var_dump. I verified that the connection id's are different after connecting to the database.
When I try to check the result of the 2nd query I get an error saying i'm calling a member function on a non-object so it must not be executing the query. If I reverse the order it will execute the other query like expected.
I can't tell if this is a PHP limitation? I'm using code igniter so it could be something related to that as well. I tested with SQL Management Studio and it works fine in separate query windows connecting multiple times to the master database. What i'm trying to accomplish is to come up with a way to run this first query then not do anything until I know that query is completed first.
Update
It appears if I turn off Persistent Database connections by disabling pconnect it will use the same connection twice.
This is the output from the connection id with pconnect set to false:
Resource id #52Resource id #52

Related

How to pass two connections to mysqli_query for running SELECT INTO query with two different server databases

My query is that I need to run a SELECT INTO query from One Server Database Table to Another Server Database Table. For that I am having two different mysqli_connect() connections so how can I run this query through mysqli_query as far as I know we can pass only one connection to it through arguments. Or is there any other way out to perform the same.
You can first create one connection of the first database through mysqli_query() and then take that output and store it into temp variable and then create another second server connection and then manipulate your data!

mysqli->insert_id always returns 0 in PHP after move to new server

I have a number of web-apps that depend on mysqli->insert_id, it always returns 0 after having moved to a new server.
I created an isolated test script to just connect to the database, insert a record and output the result of mysqli->insert_id. It always returns 0.
I've set MYSQLI_REPORT_ALL
There are no connection or query errors
I've confirmed that it is, in fact, inserting the record into the database.
If I run the exact same insert query in the mysql shell on the same server, I can query SELECT LAST_INSERT_ID() and it gives me the last inserted ID as expected.
Is there a PHP or MySQL configuration I'm missing?
EDIT: I know they're supposed to be code-compatible, but the server is actually running MariaDB if that makes any difference.

MSSQL_query in PHP Returning Diffrent Results than SQL Native Sproc

Anyone ran into this one before?
I have a Stored Procedure in SQL with the following Parameters :
exec PaySummary 'DemoTest', 'DemoTest-Mohn-00038', '5/14/12', '5/27/12', 'manager', 'DemoTest-Boyd-00005'
And the following MSSQL Query in PHP running the exact same query.
private function dataTest(){
$strSQL = 'exec PaySummary \'DemoTest\', \'DemoTest-Mohn-00038\', \'5/14/12\', \'5/27/12\', \'manager\', \'DemoTest-Boyd-00005\'';
$a = mssql_query($strSQL);
echo $strSQL;
while($row=mssql_fetch_array($a)){
var_dump($row);
}
}
When run in SQL for this query I will get 3 results...
When run in PHP through SQL I get 2 Results...
Is there any run time settings (Set NoCOUNT on) that you must set on a SQL Stored Procedure to ensure accuracy of the output of results? Or is there a known issue with passing date parameters that would impact the results of a date driven stored procedure?
Microsoft-IIS/5.0 / PHP/5.2.5 / SQL Server 2008 R2 (Where the stored procedure is executed).
For anyone in this same situation... It is caused by the NULL_CONCAT_NULL (or whatever) option in SQL. This one flag can make a stored procedure run a little bit differently depending on how you use concat etc. A good way to solve this problem is via an ISNULL around a lot of your items which seemed to get rid of the issue of getting different results.
Further another option if you do not want to fix your sprocs is to check the path that sql is going through (TCP/IP etc). I noticed when watching the audits that some settings were wildly different depending on the port that sql was running through.

PHP/MySQL returns no result and no error

I have a single MySQL query created from PHP 5. The Query has 3 SELECT and 2 JOIN clauses. It accesses two databases on a single host using one connection and db1.table1 db2.table2 techniques. I echo the query before running. In PHP the query returns no result and does not error. When I copy and paste the echoed query into SQL in PHPMyAdmin, it returns the correct result.
Why is PHP different to the SQL part of PHPMyAdmin and does anyone have any suggestions about getting it to work in PHP?
Why is PHP different to the SQL part of PHPMyAdmin
It's not.
You're connecting to the wrong database, writing the wrong query, or checking for errors incorrectly.

SQL server transactions in PHP

I'm trying to grasp the idea of transactions fully. Therefore the following question... (ofcourse newbie, so don't laugh :D )
I have set up a (simplified) transaction in PHP (using the PHP SQL driver from microsoft). I want to get the rows I'm going to delete for some extra processing later:
sqlsrv_begin_transaction($conn);
$sql = "SELECT * FROM test WITH (XLOCK) WHERE a<10";
$statement = sqlsrv_query($conn,$sql);
$sql = "DELETE FROM test WHERE a<10";
sqlsrv_query($conn,$sql);
$result = get_result_array($statement);
sqlsrv_commit($conn);
$result2 = get_result_array($statement);
1) I do get the expected result in $result but an empty array in $result2. Why?
I would expect only a result in $result2 because then the transaction has actually been executed. I guess the result in $result is a sort of 'temporary' result in memory and not actually a result from the actual database.
2) It could be that between the moment the transaction was started and the actual commit, an other query from another connection has changed the rows which match (a<10)? That means that the results I'm expecting according to $result will be different from the actual changes in the database.
Or is it that (a) the transaction occurres with an in-memory copy of the database (not affected by in-between queries from other connections), or (b) the locks obtained since the beginning of the transaction are already in action for other queries from other connections?
After typing this I'm expecting answer b....?
I'm not familiar with the sqlsrv driver, but if it works anything like most other PHP DB drivers, the result of the sqlsrv_query call is not a result set in some form of array, but a PHP resource (see http://www.php.net/manual/en/language.types.resource.php). Calling get_result_array still retrieves data from that resource, in this case the database, and it does so immediately. The COMMIT only affects writes to the database, not reads, so you see your result immediately in result1. After you commit your transaction (i.e, the DELETE), the next call correctly returns an empty result set.
I tested it out with some mysql tools (which i'm more familiar with):
1. When I start a transaction and do a 'select' of one particular record I directly get the result. Then from an other connection I delete the same record (with autocommit) it is gone for that connection but for the first connection the record is still there (I did the 'select' again without committing the transaction). Only after committing the transaction of the first connection and doing the 'select' again the record is gone.
2. When I do the same but acquire an exclusive lock for the first 'select' query then the delete query of the second connection waits until the transaction of the first connection has been committed.
Conclusion: In situation (1) for the second select query of the first connection, the database IS returning a result as it was at the moment of the start of the transaction... thus WITHOUT taking into account other (write) queries running AFTER the start of the transaction. Situation (2) is exactly the answer 2b from my original question. :)

Categories