Solved

php mssql connection

Posted on 2013-01-07
9
348 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
ID: 38751382
can you provide the exact error you are getting.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38751394
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
ID: 38751475
I dont have a mssql_select_db I put that in now and come back to you
0
 
LVL 2

Author Comment

by:beridius
ID: 38751499
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Expert Comment

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

Author Comment

by:beridius
ID: 38751581
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
ID: 38754171
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
ID: 38755156
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
ID: 38758082
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

910 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

22 Experts available now in Live!

Get 1:1 Help Now