August 14, 2014 at 11:10 am
Hi All
Can anyone assist in me converting this snippet of code from mysql to ms sql.
I am using the sqlsrv function
$check_for_con_ContractNumber = mysql_query("SELECT con_Id FROM legalcontracts WHERE con_ContractNumber='$con_ContractNumber'");
//Query to check if con_ContractNumber is available or not
if(mysql_num_rows($check_for_con_ContractNumber))
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - con_ContractNumber is available
}
August 14, 2014 at 1:34 pm
IF EXISTS (SELECT 1 FROM legalcontracts WHERE con_ContractNumber= @con_ContractNumber)
PRINT '1'; --If there is a record match in the Database - Not Available
ELSE
PRINT '0'; --No Record Found - con_ContractNumber is available
August 14, 2014 at 1:37 pm
I've never used it, but is that really what MySQL looks like?
August 14, 2014 at 1:42 pm
I could be wrong, but I think that's PHP coding; the mysql_query(...) bit matches one of PHP's functions, which prepares and executes a MySQL query against a target server. I've had to work with PHP pages linked to SQL Server, and the posted code's syntax looks quite similar.
-
August 14, 2014 at 2:15 pm
August 14, 2014 at 6:40 pm
Thanks
I will definately try it, because I nearly went crazy trying to figure out the correct syntax...
August 15, 2014 at 6:33 am
It works when i run it direct in the sql server however when i try integrating the sqlsrv function to make it work with my php code it does not work...
I cannot figure out what I am doing wrong..
Below is my updated mssql and php code
$check_for_con_ContractNumber = IF EXISTS (SELECT 1 FROM LegalContracts WHERE con_ContractNumber = '$con_ContractNumber');
$result=sqlsrv_query($conn, $check_for_con_ContractNumber);
$count = sqlsrv_num_rows($result);
if($count = 1)
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - con_ContractNumber is available
}
August 15, 2014 at 8:32 am
allanstarr023 (8/15/2014)
@LutzMIt works when i run it direct in the sql server however when i try integrating the sqlsrv function to make it work with my php code it does not work...
I cannot figure out what I am doing wrong..
Below is my updated mssql and php code
$check_for_con_ContractNumber = IF EXISTS (SELECT 1 FROM LegalContracts WHERE con_ContractNumber = '$con_ContractNumber');
$result=sqlsrv_query($conn, $check_for_con_ContractNumber);
$count = sqlsrv_num_rows($result);
if($count = 1)
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - con_ContractNumber is available
}
I believe your query needs to be enclosed in either single or double quotes.
So, your query should look like so:
$check_for_con_ContractNumber = "IF EXISTS (SELECT 1 FROM LegalContracts WHERE con_ContractNumber = '$con_ContractNumber')";
However, you're also attempting to pass in a PHP variable to the query; as such, you'll need to parameterize your PHP query statement, like so:
$check_for_con_ContractNumber = "IF EXISTS (SELECT 1 FROM LegalContracts WHERE con_ContractNumber = ?)";
$result = sqlsrv_query($conn,$check_for_con_ContractNumber,$con_ContractNumber);
I think that should do it, but it's admittedly been a few months since I last wrote this kind of code. Let us know if this doesn't pan out quite right.
-
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy