Jump to content
Guest

PHP - SQL Connection

Recommended Posts

Guest

Hello there,

I've started studying php and sometime ago I tried to establish SQL server connection however encountered an error:

php_network_getaddresses: gethostbyname failed. errno=2

 

A couple of things to consider:

- SQL works fine.

- I can connect to the SQL using the username & password given below.

 

PHP code:

<?php
$servername = ".\SQLExpress";
$username = "sa";
$password = "Y87dc#$98";
$dbname = "AccountServer";


$conn = new mysqli($servername, $username, $password);

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

Any help is appreciated!

Share this post


Link to post
Share on other sites

You cannot use mysqli extension to connect to a MSSQL server. Mysqli is used for connecting to mysql server.

For mssql you have to you mssql or sqlsrv extension.

Share this post


Link to post
Share on other sites
Guest
13 minutes ago, Wrexor said:

You cannot use mysqli extension to connect to a MSSQL server. Mysqli is used for connecting to mysql server.

For mssql you have to you mssql or sqlsrv extension.

Thanks for a quick response. I edited the code and have the same error.

<?php
$servername = ".\SQLExpress";
$username = "sa";
$password = "Y87dc#$98";
$dbname = "AccountServer";

mysql_connect($servername, $username, $password) or die('Could not connect: ' . mysql_error());

?>

 

Share this post


Link to post
Share on other sites

@qwerty

Use mssql_connect() and mssql_get_last_message() instead of mysql_connect() and mysql_error()


Share this post


Link to post
Share on other sites

Nice to see someone learning something :D.

 

You're still using mysql

Mysql != mssql

You're gonna have to learn sqlsrv ( which is the latest library for connecting php->mssql )

You could go for mssql by hacking around a little and installing the DLLs manually but I'd advise against that since its deprecated.

 

An easier way would be to learn PDO, its an abstraction layer which makes everything that much easier.

Here's what you should be using :http://php.net/manual/en/function.sqlsrv-connect.php

 

A pdo guide if you decide to go that way : http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

 

You'll just need to replace mysql with sqlsrv in the connection line and everything else remains the same.

 

Hit me up on Skype if you need any more help : iturble

Edited by Perseus

Share this post


Link to post
Share on other sites
Guest
3 minutes ago, V3ct0r said:

@qwerty

Use mssql_connect() and mssql_get_last_message() instead of mysql_connect() and mysql_error()

 

2 minutes ago, Perseus said:

...

@V3ct0r, @Wrexor thanks - it's working now :)

@Perseus, yup I've just realized haha, I'll take a look on that. Thanks!

 

In case someone comes across the same issue:

<?php
$servername = ".\SQLExpress";
$username = "sa";
$password = "Y87dc#$98";
$dbname = "AccountServer";

mssql_connect($servername, $username, $password) or die('Could not connect: ' . mssql_get_last_message());
?>

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...