0
$serverName = 'servername';   
$uid = 'username';     
$pwd = 'password'; 


$conn = new mysqli($serverName, $uid, $pwd );    
if (!$conn) {
    echo "Connection failed: " ;
}
else
{
echo "Connected successfully";
}

This is my code. It gets connected to the database. I just want to confirm the code is right, because when i try doing this

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

it throws me connection failed error. So, is my code connected to the server or not? because when i run a query it does not show me anything.

Code for the query:

$sql = "SELECT max([line_nbr]) FROM [dbo].[so_audit]";

$res = $conn->query($sql);
var_dump($res);

please advise

prachi
  • 21
  • 6

1 Answers1

0

you are not trying to connect to sql server u should use sqlsrv_connect instead of mysqli so u need to specify the server name and an array containin connection info for that your code should look like this :

 $srv ="servername"
    $info=array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
    $conn = sqlsrv_connect( $srv, $info);
    if (!$conn) {
        echo "Connection failed: " ;
    }
    else
    {
    echo "Connected successfully";
    }
bg1
  • 54
  • 8