Hello im developing an application where data comes from MSSQL database. Here using php odbc connection i get all the needed data to work with app. But now i have a problem:
Im looking for:
All data from mssql (assume) mssql_Table_A will be listed in this app, each row will be given with checkbox so user can check the data required, after checking When he hit save checked data will be stored in local MySQL database (assume) mysql_table_A.
So next time when he again wants few more data, this app will list all the data from mssql_table_A which dont exists in mysql_table_A.
Problem is:
If both the dataflwo were from MYSQL i would have done this where easily by using mysql select query
mysql_query("SELECT * FROM mysql_table_a WHERE NOT EXISTS (SELECT * FROM mysql_table_B WHERE thisID != thatID)")
But here how do i check data exists in mysql table and list the data from odbc mssql database.
Please help me to resolve this problem.
Thank You..
You need to use mssql_, sqlsrv_ or PDO functions to connect to MSSQL. mysql_ functions are strictly for MySQL.
Microsoft SQL Server Driver for PHP
Microsoft SQL Server
Microsoft SQL Server Functions (PDO_SQLSRV)
You will need two separate connections if you are fetching data from both.
Alternatively, you can connect to MySQL from MSSQL using an ODBC connection and perform your query on the MSSQL side.
The other answers have told you how to connect the two databases but based on your comments you still want to know how to do the comparison.
I have to assume you've got at least one column which you can uniquely identify rows in table_a which are not in table_b. If you link mysql in MS SQL Server like one answer said you can
select * from mssql.table_a where unique_column not in (select unique_column from mysql.table_b)
but this works because mssql is linked to mysql in mssql i.e., you can do one query and mssql will do the join for you.
If for some reason you cannot or don't want to link mysql to ms sql via a linked table the easiest way would be if you had some sort of timestamp in table_a. Let's say every record written into table_a has a timestamp of when it was inserted:
mysql connection does select max(timestamp) into X from mysql.table_b
mssql connection does select * from mssql.table_a where timestamp > X
There may be other ways if we knew a bit more about your data.
Related
I am using laravel with mssql and getting this error with simple group by query.
RetailersModel::groupBy('state')->get();
And it returns me error.
I have migrating database server from mysql to mssql.
Mysql is running fine but error occur when try to get record from ms-sql database server.
I have also set strict false in mysql and mssql config in database php.
Thanks in advance.
RetailersModel::groupBy('state')->get();
Try to execute this query
RetailersModel::select('*','state', DB::raw('COUNT(state) as state'))->groupBy('state')->get();
I would like to use PDO ODBC to get datas from .mdb file. This works fine when querying on "normal" tables, however it always fails on linked tables, whatever the query is.
Connexion :
$pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdb;SystemDB=$mdw;", $user, $pass);
Ex :
A normal table "test" (id, name)
Query : SELECT * FROM test
It works, I get the expected rows
A linked table "linked_test"
Query : SELECT * FROM linked_test
It doesn't return anything
But on attached/linked/synchronized tables i can't get any results, the PDOStatement just return the queryString, no result to fetch.
These tables are synchronized to a Oracle Database.
Then the question is how should I proceed to get results from theses tables ?
Thanks.
Sorry for my bad english...
Unfortunately, what you are attempting simply won't work from PHP. You will need to create a separate connection to the Oracle database and pull the information directly from there.
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.
i have tableA in sql database ,
and tableB in mysql database ,
How to write the join and which function should i use for that(myssql_query or mssql_query )
Thanks
You can't do that unfortunately. Even if you connected to both via ODBC, you'd still have two separate connections. Besides MySQL knows nothing about MSSQL, and MSSQL knows nothing about MySQL.
An additional layer of abstraction would be required, but it would possibly be very inefficient.
So far as I know it is not possible with default PHP (mysql and mssql) functions, but I'm pretty sure that it is possible with ODBC on your machine.
With ODBC you can make cross DB connections between MySQL and MSSQL. So I think you can create a query like this:
SELECT
MYSQL.db.tbl_x.*
LEFT JOIN
MSSQL.db.tbl_y
ON
MYSQL.db.tbl_x.id=MSSQL.db.tbl_y=id
If you only would like to copy some data, I recommend Navicat.
How do I get the last insert id from a database using a ODBC connection?
I'm looking for a solution similar to the mysql_insert_id() function.
SELECT ##IDENTITY AS ID
If you're using databases with PHP I strongly recommend using PDO (simple database wrapper for a lot of common database engines, more and more supported all the time, part of PHP canon), and hence use PDO::lastInsertId if your database supports the equivalent of mysql_insert_id.
Don't use "SELECT max(id) FROM table;" as it can result in seriously freaky and hard-to-find bugs later on.
* **UPDATE : Ok, you're using ODBC, and I suspect you're after odbc_cursor. I still stand by the strong recomendation to use PDO, as it has an ODBC driver. (ODBC in my eyes is an grumpy bitter old man who mumbles under his breath driving his truck that's falling apart, as the hip and effective PDO guys race past in their sexy VOLVO S90's)
It depends on the database type, but look into the SEQUENCE syntax for your rdbm.
I used "SELECT ##IDENTITY AS LastID", while working with PHP/MSSQL, through ODBC. That brought some issues under SQL 2005 server (or was it 2000?).
Either way if you do like this:
SET NOCOUNT ON
[NOW INSERT YOUR INSERT SQL QUERY]
SELECT ##IDENTITY AS LastID
you should be just fine in any MSSQL server version.
"SET NOCOUNT ON" will prevent sql server from sending messages like '1 rows inserted', which will keep your script working properly.
If you have MySQL under ODBC - you can use the next query:
"SELECT LAST_INSERT_ID( );"
It have to be executed mmediately after executing INSERT-query.
For other database - use other specific queries...
Use in MySQL
SELECT *
FROM table_name
ORDER BY Id Desc
LIMIT 1
Use in SQL Server
SELECT top 1 *
FROM table_name
ORDER BY Id Desc
Are you inserting rows into your database via PHP? If so, perhaps you can generate a unique primary key using uniqid() - then you will know the ID without having to query the database.
If it is not possible to update the key type, perhaps you can still insert a unique id when you do the inserts, so you could do a query like this:
SELECT id FROM mytable WHERE rowid = '$myuniqueid'
That way you're ensuring that you're pulling back the correct ID from the database - a much better solution than MAX(id).
Using
SELECT max(id) FROM table;
should be fine from inside a transaction, or does ODBC not support transactions?