List table names - php

I'm in the process of trying to migrate my ASPNET site to Django. All my current data is tied up in MS SQL Server on a shared host that (helpfully) doesn't support python... I could use my existing code to export all the data but I've also stopped using Windows. The site is mainly compiled VB.NET so I'd need to install Windows and Visual Studio and then figure out what I'm trying to do... I don't like that plan.
Rather than go through that headache, I'd like to use PHP to export the entire database (or a table at a time) to JSON. SimpleJSON in Python will make it ludicrously easy to import so it seems like a plan.
So far, so good. In PHP I've managed to connect to the SQL Server and perform simple queries, but I need a list of tables so I know what data I need to copy. I want all the tables because there are legacy tables from when I rewrote the site about three years ago, and I'd like to keep that data somewhere...
So first thing: Does anybody know the SQL query for listing all tables?
I've tried mssql_query('sp_tables'); but this returns a strange list:
mydatabasename
mydatabasename
dbo
dbo
syscolumns
syscolumns
SYSTEM TABLE
SYSTEM TABLE
Secondly: In your opinion, would I be better off writing each table dump to its own .json file or should I keep it all together?
Thirdly: Am I going about this the wrong way?

Select table_name from information_schema.tables
for any given table you can get the metadata with
sp_help tablename

You do query with :
SHOW TABLES;
(you need to select DB before this.)

you can try this
SELECT * FROM information_schema.tables

Related

Find the diff between a database and a mysql file

I have a database and a mysql file of the same database with some modifications(Some tables might have been added, some might have been dropped, table structures, some tables definitions might have been changed). How to find the changes between the two databases and their tables using PHP (structure only)?
I've done this sort of some time ago. It boils down to having two databases, dump them to a file and then compare them with diff, adjusted for known differences like database names.
There are also tools out there that can do this interactively by querying two databases and allowing to sync differences.
I don't know of a tool that compares a dump file with a database, though.
Here is a MySQL procedure that does exactly that: http://www.artfulsoftware.com/infotree/qrytip.php?id=624
Hope it helps (PS: there are a lot of useful queries here: www.artfulsoftware.com/infotree/queries.php )
PS: To get the data in PHP, just run the query and grab the info, then do whatever you want with it.
PS2: Import your old database dump into another database, and then use this procedure to compare the databases.

Cross Join Progress Database via OpenEdge ODBC and PHP

I currently have 2 ODBC connections set up on my web server. One that connects to the our enterprise QAD database and another that connects to our custom database used to extend our database. In this paticular example I have my employee records in the QAD database, and then an employee number in another table in the custom database.
Is there any way for me to set up a cross join between the two odbc connections in php so that I don't have to loop through the results of the first query and send several queries based on the returned results to tie my records together in a php array?
The best i've been able to come up with is to build an IN clause from my first query from our custom database, send the second query to the QAD database, and then do an array merge in php. However, this is an extremely slow process compared to a normal SQL join.
Not sure if you've already found a solution to this but there is a Progress article on how to do this.
Quick Guide To Setting Up MultiDatabase ODBC Connectivity
I had a similar requirement - I wanted to create a join between a table in the primary QAD database and a custom table in our custom database. I have tested this and it works well although my setup is slightly different. I needed to connect to QAD from Microsoft SSRS to create reports against the QAD data - I needed to create some reports that the standard QAD report designer could not handle.
I have tested this on Progress 10.1c (this method is only supported in 10.1b+).
So the steps I took were:
Create the oesql.properties config file as per the article relevant to the primary and custom databases.
Create the ODBC System DSN on the client machine (in my case a Windows Server 2008 R2 machine running SQL Server 2008 R2 with SSRS) with the additional database references as per the article.
Create a Linked Server in SQL Server via the ODBC DSN
Create a view which uses the OpenQuery syntax to extract data from QAD (in my case this was created in the ReportServer database) via the linked server.
Create standard T-SQL query using the view in point 3 as the data source. This was ultimately the datasource for my SSRS report.
I believe it is important that the bit versions of the OS/Database and the ODBC drivers match but haven't confirmed this yet.
Whilst my requirement is different to your's ultimately it's the QAD server config and ODBC setup that's key. As long as your PHP client can perform a similar capability in terms of the OpenQuery command then you may get this working. I don't have any experience with PHP so can't help you there.
It seems a bit convoluted but actually works very well and in a lot of cases actually outperforms querying data using QAD browses!
Hope this helps.
Edit:
Here's a sample of an OpenQuery command - you can see that the table joins work in the normal way but just require and additional piece in the table reference.
CREATE VIEW [dbo].[vQADData] AS SELECT * FROM OPENQUERY(LinkedServerName,
'
SELECT custTable.item_date AS DESP_DATE, so_mstr.so_site AS SITE, so_mstr.so_po AS PO_NO, so_mstr.so_inv_nbr AS INV_NO,
ad_mstr.ad_name AS ADNAME, ad_mstr.ad_city AS ADCITY, ad_mstr.ad_state AS ADSTATE
FROM customdbname.pub.customtable custTable
INNER JOIN pub.so_mstr ON so_mstr.so_nbr = custTable.so_nbr
INNER JOIN pub.ad_mstr ON ad_mstr.ad_addr = so_mstr.so_ship
INNER JOIN pub.sod_det ON sod_det.sod_nbr = custTable.so_nbr
WHERE so_mstr.so_site = ''SiteName'' AND so_mstr.so_shipvia = ''SHIPPER'' AND custTable.item_date IS NULL
')
Then just access the view using normal SQL syntax.
SELECT * FROM vQADData
Thanks to Tiran for his suggested solution. For those people that are trying to reference multiple tables via SQL server as Tiran was doing, I have additional input.
I'm trying to pull data from multiple sources (Progress), same table structure at the same time and insert it into our data warehouse (SQL Server). So I'm just trying to do a union of multiple same structured tables in different databases. Tiran's solution started me down that same path but the linking of Progress databases was a cumbersome process that required me to find a Progress DBA with 2-3 days free time (his quote) to put this together. When I spoke with people at Progress directly, they also pointed out that if I created a view with a union on the Progress side, it would sequentially extract the data from each source in the view, not simultaneously. However, this led me to another discovery that looks like it's going to solve our needs and totally skips dealing with linking tables on the Progress side.
Here's an example with three sources, same tables (this should work for cross source joined different tables as well). All names here are provided just for clarity in the examples.
Source 1 - Table_A
Source 2 - Table_A
Source 3 - Table_A
Create an ODBC connection to Source 1 named source1.
Create an ODBC connection to Source 2 named source2.
Create an ODBC connection to Source 3 named source3.
(Note, you typically want to be sure to set the connection setting to Read Uncommitted).
In SQL Server, create linked Server connections to each Source.
ls_source1
ls_source2
ls_source3
In your SQL Server database that you need to reference the Progress databases in, create a view joining the three different linked server connections together using a union. The linked server references will each need to use openquery. This example using select * from each linked server source presumes that all columns are named and structured the same from each source.
CREATE VIEW table_name_v as
SELECT *
FROM
(SELECT *
FROM OPENQUERY(ls_source1,
'select *
from source1.dbo.Table_A
')
union
SELECT *
FROM OPENQUERY(ls_source2,
'select *
from source2.dbo.Table_A
'
union
SELECT *
FROM OPENQUERY(ls_source3,
'select *
from source3.dbo.Table_A
'
)
) x
With the view created, you can now query all three tables in different Progress sources at the same time. No extra set up on the Progress side is necessary.
There is an important caveat that I'm currently working on a work-around for. If you are on a 64bit machine using 64bit SQL Server, you need to use a 64bit driver to connect to the Progress database with the linked server option. My needs require I have both the 32bit and 64bit drivers on the same machine and have run into issues with that as apparently they don't play nice together when on the same machine. I have been able to install both 64bit and 32bit drivers on the same machine (there was a glitch in Progress' website that was supposed to send me a link for that driver but I was able to get someone there to direct me to the correct place to retrieve the 64bit odbc driver. The average person should not need both drivers and can just use the 64bit. As an alternate work around, if I'm not able to get both drivers co-existing on the same machine, I've found and confirmed that the company Connx provides a driver that provides a 64bit/32bit bridge that resolves that issue for me. Ideally though, no third party software will be necessary.
A new issue has cropped up now unfortunately, as I the linked servers that I set up and were using are no longer functioning properly. Two steps forward, one step back....
Just thought I'd share my findings as I'm sure there are others looking.
Short Answer: You can't JOIN tables between two connections.
Scenarios: (all of them in one single connection)
By default, in most databases, you can Join tables in different schemas by prefixing the schema name before the table, like this:
(...) FROM defaultDB.TableA INNER JOIN extensionDB.TableA ON ({Condition}) (...)
Depending on your Database (I don't know about Progress DB deeply), you may not be able to join Tables that belongs to schemas in different servers.
Joining tables in different databases (Eg: Progress x MySQL) it's even more complicated. I've heard about Oracle Gateway, a proprietary solution that (not really sure) could achieve this last scenario.
In summary:
If your situation does not fit the first scenario (which points to the most obvious approach), I guess the shortest solution would be profiling your code and optimize possible performance bottlenecks. Adapting your code for parallel processing could be a bolder improvement.

Displaying results of many sql queries without writing PHP code?

Sorry if this question might sound stupid to you guys, but am total newbie to programming, apart from knowing SQL, the thing is i have been given a MYSQL database containing various information about kids diseases and a web interface written in php to create reports from the database that can be accessed via the interface. there are almost 25 different variables that need to be computed in the report, i have written sql queries to compute all these values, but i don't know anything about PHP, if i have all these queries isn't there a way for me to combine all these sql queries to be display results on a webpage and come up with this report without writing PHP code?
Thanks for your
again very sorry if this is too basic.
As mr_jp suggests, phpmysqladmin provides a simple front end for running queries, but also changing the data and modifying the schema. Although you can restrict named users to only have SELECT privilege, they'll still need to know SQL to run the queries.
It's not that hard to build a front end to take a set of parameters, substitute them into a SELECT statement and send the output to a formatted table. There are lots of datagrid tools (e.g. phplens, phpgrid, have a google for 'mysql datagrid' for more) which will handle the formatting of a MySQL resultset (or just download it as CSV - your browser should be able to transfer the data into your spreadsheet program automatically).
There are a couple of report generators for PHP - but the last time I looked at this in any depth, I wasn't overly impressed.
Your web host would probably have phpmyadmin installed. Try getting access from the web host.
You can enter your queries there and export the results as html, csv, excel and others.
You could write Python. Or Ruby. Or something you know. ;-)
But you need something to output your queried data.
If you just want to check the results by yourself without having the needs to publish that directly, you might use some MySQL query browser or administrator like phpMyAdmin or the MySQL Workbench. Those tools allow you to query the database but display the returned data only as raw tables. If you need some styling or your own layout, you'll have to use an own application or edit the exported data manually (e.g. using a CSV export and re-open it using some spreadsheet application like Excel or Calc).
The combination PHP + MySQL is a very popular one and it's highly recommended that you use them together.
The code that you will need to write in order to display that information using PHP is pretty straightforward and not very hard. If you do know some basic programming concepts, you can learn to do that in a matter of hours. PHP is well known for its extremely accessible learning curve. There are thousands of code samples online that you can look at to see how this is done.

Should I use phpmyadmin or php to add content a to a database?

My knowledge of MySQL is very basic here... so I'm just wondering if I want to create a table in a database, and add rows to that table, is it best to do so through phpmyadmin or should I do so in a PHP file?
Of course you can do it programmatically, maybe for your project you'll have to do it later anyway. But to get used to this whole SQL stuff, maybe it would be better to use some administration tool, like the mentioned phpMyAdmin or MySQL Workbench. I wouldn't recommend the commandline tool for starting, except you like a puristic commandline environment.
I just found this phpMyAdminDemo. Maybe it's good to start with, if you really want to use PHPMyAdmin. But if you don't have to, I would recommend to use Mysql Workbench, because it has a nice user interface and I hope it's relatively easy to deal with. A really nice feature is, that you can create diagrams of your database in the GUI, and forward it to the database. Even if you modify the diagram (e.g. adding columns), you can synchronize it with the database with only a few clicks. Additionally to that you can enter and edit data with Workbench as well.
So you might have a basic database structure then - after you struggled through some select statements in PHP like in PHP MySQL Select you will maybe finally get to the point where you want to: Like creating tables with PHP and MySQL or inserting data with PHP and MySQL
[Edit] I re-read your question as your title and question don't match. To answer your question; Creating the database schema is what phpmyadmin was made for. For managing data see what I wrote below.
Depends on your situation. If there's only you and just you managing the content then it can be an easy way to insert and edit data quickly. If you want to do anything advanced, for example:
WYSIWYG (HTML editing) or
Validation
then you'll need to make something yourself. I wouldn't recommend you have a client using a CMS to edit through phpmyadmin as they're given too much power and could screw things up.
I'd suggest using phpmyadmin as its pretty easy for novice users. Here is a very detailed article to add a table in phpmyadmin - http://php.about.com/od/learnmysql/ss/create_tables.htm

How to convert query from phpMyAdmin SQL Dump to an sql server legible query

I have undertaken a small project which already evolved a current database. The application was written in php and the database was mysql.
I am rewriting the application, yet I still need to maintain the database's structure as well as data. I have received an sql dump file. When I try running it in sql server management studio I receive many errors. I wanted to know what work around is there to convert the sql script from the phpMyAdmin dump file that was created to tsql?
Any Ideas?
phpMyAdmin is a front-end for MySQL databases. Dumping databases can be done in various formats, including SQL script code, but I guess your problem is that you are using SQL Server, and T-SQL is different from MySQL.
EDIT: I see the original poster was aware of that (there was no MySQL tag on the post). My suggestion would be to re-dump the database in CSV format (for example) and to import via bulk insert, for example, for a single table,
CREATE TABLE MySQLData [...]
BULK
INSERT MySQLData
FROM 'c:\mysqldata.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
This should work fine if the database isn't too large and has only few tables.
You do have more problems than making a script run, by the way: Mapping of data types is definitely not easy.
Here is an article about migration MySQL -> SQL Server via the DTS Import/Export wizard, which may well be a good way if your database is large (and you still have access, ie, not only have the dump).
The syntax between Tsql and Mysql is not a million miles off, you could probably rewrite it through trial and error and a series of find and replaces.
A better option would probably be to install mysql and mysqlconnector, and restore the database using the dubp file.
You could then create a Linked Server on the SQL server and do a series of queries like the following:
SELECT *
INTO SQLTableName
FROM OPENQUERY
(LinkedServerName, 'SELECT * FROM MySqlTableName')
MySQL's mysqldump utility can produce somewhat compatible dumps for other systems. For instance, use --compatible=mssql. This option does not guarantee compatibility with other servers, but might prevent most errors, leaving less for you to manually alter.

Categories