Solved

php mssql connection

Posted on 2013-01-07
9
346 Views
Last Modified: 2013-01-09
I have a connection to MSSQL database which I use mssql_query and mssql_fetch_assoc

I have problem when I use a join in the statement.
when just connecting to a single table in the SQL part
SELECT        id, sName, iStatus
FROM            [SQL01].[dbo].[KPIDay]

Open in new window


If i do not put the [sqldatabase].[dbo].[tablename]
I get an internal error

but I am stuck with is statement  work in sql builder but no in php

SELECT        Lead_T.sName AS Expr1, LeadSource_T.sName, TransferClient_T.sName AS Client, TransferSubscription_T.sName AS Subs, LeadTransfer_T.zXUpdtdWhn AS Date, 
                         LeadTransfer_T.zXUpdtdBy AS [Updated By], TransferClient_T.id
FROM            TransferClient_T INNER JOIN
                         LeadSource_T INNER JOIN
                         LeadTransfer_T ON LeadSource_T.id = LeadTransfer_T.FKiSourceID INNER JOIN
                         Lead_T ON LeadTransfer_T.FKiLeadID = Lead_T.id ON TransferClient_T.id = LeadTransfer_T.FKiClientID INNER JOIN
                         TransferSubscription_T ON LeadTransfer_T.FKiSubscriptionID = TransferSubscription_T.id
WHERE        (LeadTransfer_T.zXUpdtdWhn BETWEEN CONVERT(DATETIME, '2012-10-29 00:00:00', 102) AND CONVERT(DATETIME, '2012-10-29 23:59:59', 102)) AND 
                         (TransferClient_T.id = 27)
ORDER BY TransferClient_T.zXUpdtdWhn, Date, LeadSource_T.sName DESC

Open in new window

not sure where I need the [sqldatabase].[dbo].[tablename]
0
Comment
Question by:beridius
9 Comments
 
LVL 17

Expert Comment

by:Gaurav Singh
Comment Utility
can you provide the exact error you are getting.
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Have you selected the database before you query?

mssql_select_db("SQL01");

$results = mssql_query("SELECT id, sName, iStatus FROM KPIDay");
0
 
LVL 2

Author Comment

by:beridius
Comment Utility
I dont have a mssql_select_db I put that in now and come back to you
0
 
LVL 2

Author Comment

by:beridius
Comment Utility
I just get a blank screen no errors but when I try and echo a column I dont get anything
$query_Recordset1 = "SELECT        Lead_T.sName AS Expr1, LeadSource_T.sName, TransferClient_T.sName AS Client, TransferSubscription_T.sName AS Subs, LeadTransfer_T.zXUpdtdWhn AS Date, 
                         LeadTransfer_T.zXUpdtdBy AS [Updated By], TransferClient_T.id
FROM            TransferClient_T INNER JOIN
                         LeadSource_T INNER JOIN
                         LeadTransfer_T ON LeadSource_T.id = LeadTransfer_T.FKiSourceID INNER JOIN
                         Lead_T ON LeadTransfer_T.FKiLeadID = Lead_T.id ON TransferClient_T.id = LeadTransfer_T.FKiClientID INNER JOIN
                         TransferSubscription_T ON LeadTransfer_T.FKiSubscriptionID = TransferSubscription_T.id
WHERE        (LeadTransfer_T.zXUpdtdWhn BETWEEN CONVERT(DATETIME, '2012-10-29 00:00:00', 102) AND CONVERT(DATETIME, '2012-10-29 23:59:59', 102)) AND 
                         (TransferClient_T.id = 27)
ORDER BY TransferClient_T.zXUpdtdWhn, Date, LeadSource_T.sName DESC
";
mssql_select_db('LM',$conn);
$Recordset1 = mssql_query($query_Recordset1) or die();
$row_Recordset1 = mssql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mssql_num_rows($Recordset1);

Open in new window

not sure where I am going wrong
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Add something inside die(), like die("Problem querying!");
0
 
LVL 2

Author Comment

by:beridius
Comment Utility
I put print print var_dump(mssql_get_last_message());


and get
string(39) "Invalid object name 'TransferClient_T'."
0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 250 total points
Comment Utility
I think this is wrong

FROM            TransferClient_T
INNER JOIN   LeadSource_T   /*(no mention of connection field) */
INNER JOIN   LeadTransfer_T ON LeadSource_T.id = LeadTransfer_T.FKiSourceID
INNER JOIN   Lead_T ON LeadTransfer_T.FKiLeadID = Lead_T.id ON TransferClient_T.id = Transfer_T.FKiClientID
INNER JOIN    TransferSubscription_T ON LeadTransfer_T.FKiSubscriptionID = TransferSubscription_T.id
But that should not generate the error message you have generated.
At the same it has been often mentioned that mssql_get_last_message is not a reliable and
indicative of the reason for failing query/operation.
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
Comment Utility
Is the TransferClient_T table not inside the database you selected, or is it owned by a different user? You can use owner.tablename to specify if need be.
0
 
LVL 2

Author Closing Comment

by:beridius
Comment Utility
I have just checked the connection to that table I put [databasename].[dbo].[tablename] and its working now thanks guys for all your help
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now