?
Solved

How can I out put multiple/nested queries in SQL using PHP and SQL Server?

Posted on 2012-03-21
4
Medium Priority
?
677 Views
Last Modified: 2012-03-22
Hey guys, I have a query which I have tested in SQL servers management studio, but I cannot get it to work in PHP as it tells me there is an error.

Fisrt of all here is my query which I know works as I wrote it in the management studio:

$query3 = "select * from product_catalogue where catalogueid =(select catalogueid from products where productid = '" . $productid . "')  (select * from products where productid = '" . $productid . "')";	

Open in new window



Here is the rest of the query where I feel there is a problem outputting it:


$result3 = sqlsrv_query( $conn, $query3, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
    
		if( $result3 === false)
		{
			 echo "Error in query preparation/execution.\n";
			 die( print_r( sqlsrv_errors(), true));
		}
		

			while( $obj = sqlsrv_fetch_object( $result3)) 
						{
							
							$prod_image = $obj->picturem;
							$prod_id = $obj->catalogueID;
							$prod_description = $obj->description;
							$prod_price = $obj->product_price;
							echo "<p>$prod_id" ;
							echo "<br/>$prod_description" ;
							echo "<br/>&pound;$prod_price";	
							echo "<br/><br/><img src='$prod_image'  alt='$prod_name' title='$prod_name' width='200' height='400'/>"; 
							echo "<br/><br/>";

Open in new window




Here is the error I get when running that query:


Error in query preparation/execution. Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 16954 [code] => 16954 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Executing SQL directly; no cursor. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Executing SQL directly; no cursor. ) [1] => Array ( [0] => 01S02 [SQLSTATE] => 01S02 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 10.0]Cursor type changed [message] => [Microsoft][SQL Server Native Client 10.0]Cursor type changed ) ) 

Open in new window




I have tried solving this by reading up on cursors but I had no luck, I don't even know if it is possible to do this type of query.


I appreciate any help.

Thanks!
0
Comment
Question by:deucalion0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 37747203
I do not recognise the syntax of your query, perhaps the management studio is more forgiving than I am. Shouldn't you be using a join?

$query3 = "SELECT * FROM pc.product_catalogue AS pc,
INNER JOIN p.products AS p
ON pc.productid =  p.productid 
WHERE pc.catalguied = $productid"; 

Open in new window

0
 

Author Comment

by:deucalion0
ID: 37751379
Thanks for your reply MunterMan, I tried your suggestion but I am receiving errors, I even tried it in the management studio, this is what I put into the management studio:

SELECT * FROM pc.product_catalogue AS pc,
INNER JOIN p.products AS p
ON pc.productid =  p.productid 
WHERE pc.catalguied = 1

Open in new window



The error I received was:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'INNER'.

Thanks for helping me with this.
0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 2000 total points
ID: 37751820
There is a comma on the end of the first line. My bad.
SELECT * FROM pc.product_catalogue AS pc
INNER JOIN p.products AS p
ON pc.productid =  p.productid 
WHERE pc.catalguied = 1

Open in new window

0
 

Author Closing Comment

by:deucalion0
ID: 37751876
Thanks for all your help and helping me get my code working, appreciated!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

743 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