Question

sushant sagar on Tue, 21 Oct 2014 07:06:46


I'm trying to connect to sql server on azure through php.

The code I'm using is:

<?php
$myServer = "my_servaer_name.database.windows.net";

$myUser = "your_name";
$myPass = "your_password";
$myDB = "name_db"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with

$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 

//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'"; 

//execute the SQL query and return records
$result = mssql_query($query);

//close the connection
mssql_close($dbhandle);
?>

but when this php file runs the server gives 500 internal error

I tried using mysql_connect, mysql_select_db instead of mssql_connect and mssql_select_db

I didn't get 500 server internal error but on debugging i found that the code was not able to connect to the server.

Please help.

How to connect to MS SQL Server database

Print

Below is the code for connecting to a MSSQL Server database.

<?php
$myServer = "localhost";
$myUser = "your_name";


sushant sagar


Sponsored



Replies

Mekh Subba on Tue, 21 Oct 2014 08:42:26


Hi,

Please have a check on the below links which talks about how to connect to Azure SQL database using PHP.

http://msdn.microsoft.com/en-us/library/azure/ff394110.aspx

http://azure.microsoft.com/en-us/documentation/articles/sql-database-php-how-to-use/

Regards,

Mekh.

sushant sagar on Wed, 22 Oct 2014 07:36:02


Thank you @Mekh

I got it done.

$connectionInfo = array("UID" => "myuser@server", "pwd" => "mypass", "Database" => "mydb_dba",
"LoginTimeout" => 30, "Encrypt" => 1);

$serverName = "tcp:server.database.windows.net,1433";
$conn = sqlsrv_connect($serverName, $connectionInfo);

Mekh Subba on Wed, 22 Oct 2014 09:13:47


Hi,

I am glad that you have the issue sorted.

Please let us know if you have any other issues.

Regards,

Mekh.

sushant sagar on Wed, 22 Oct 2014 11:05:15


@Meck

the following code is not working for me

$to = $email; 
$subject = "Microsoft Developer Group";
$message = "Thank you for registering with us..";
$from = "xyz@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

please help me in this. i am not able to send mail through php

while writing parameters in insert query do we have to put the varchar parameters in quotes and numeric ones without them like we do in oracle database or its ok anyway.

 


sushant sagar