I am trying to fetch a set of fields from a Database on MSSQL Database on SQL Server 2012. This is a remote server and I am trying the following piece of code.
//MSSQL Server for retrieving the Member name from Member ID:
//mssql.secure_connection = On
// Need to upload ntwdblib.dll from net
$myServer = "IPAddress/SQLExpress "; // host/instance_name
$myUser = "ID"; // username
$myPass = "pass"; // paasword
$myDB = "dbname"; // database name
// connection to the database
$dbhandle = mssql_connect($myServer, $myUser,$myPass)
or die("Couldn’t connect to SQL Server on $myServer"). mssql_get_last_message();
// select a database to work with
$selected = mssql_select_db("sportsclub", $dbhandle)
or die("Couldn’t open database $myDB");
echo "You are connected to the " . $myDB . " database on the " . $myServer . ".";
$query = "SELECT first_name, last_name";
$query .= "FROM members ";
$query .= "WHERE member_id='".$row['member_id']."'";
// your database query
$result = mssql_query($query);
while($row = mssql_fetch_assoc($result))
{
echo "<td>" . $rows["first_name"] . $rows["last_name"] . "</td>";
}
// close the connection
mssql_close($dbhandle);
//Ended MSSQL Connection
It simply does not connect to the sql server. It gives the error: Couldn’t connect to SQL Server on IPAddress/SQLExpress
I tried checking all configurations like TCP/IP through SQL Server Config management.
Can someone please help?
Ensure remote connections over named pipes are enabled and make sure you're using a backslash before the instance name:
$myServer = "IPAddress\SQLExpress ";
If you'd like to enable connection over the default port (1433) this answer will help.
Related
I am using two domain name ('domain1','domain2') and 2 server('server A' and 'server B'). 'domain1' is the Main domain. 'domain2', i am using as backend like, to store data. So the data will be stored in server B. When i use 'domain1' in browser it will display the data which is stored in 'server B' . I am not using 'server A' .
I have done some code. Please go through it and let me know what correction i have to do. The code is below,
//these hostname,dbname,user and password from server B,(is this correct?)
$hostname = "1.1.1.1" ;
$database = "db_B" ;
$user = "u_B" ;
$pasword = "u_pas" ;
$conn = mysqli_connect($hostname,$user,$pasword,$database) ;
if (!$conn)
{
die ("Error connecting to the database because : " .
mysqli_error($conn)) ;
}
In domain1 config.php file i have written the following code. It's working for me.
$servername = "domain2.in" ;
$database = "db_B" ;//server B's database name
$user = "u_B" ;//server B's database's(db_B) user name
$pasword = "u_pas" ;//server B's database's(db_B) password
$conn = mysqli_connect($servername,$user,$pasword,$database) ;
if (!$conn)
{
die ("Error connecting to the database because : " .
mysqli_error($conn)) ;
}
after this, add "server A"'s Remote MySQL hostname in "server B"'s Remote MySQL.
I have write a local website via php, and now I put it on host and domain and i have some problem to connect to the mysql and I got folowing eror:
Connection failed: Access denied for user ''#'176.31.33.116' (using password: NO)
my code are according to below:
part of my index.php:
<?php
$DB_HOST = 'ghadir1.shatelhost.com';
$DB_USER = '';
$DB_PASS = '';
$DB_NAME = 'minewebs_sahar';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
} else {
$sql = "SELECT * FROM users AS u JOIN scores AS s ON (s.user_id=u.id) ORDER BY score DESC";
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
?>
<li class="list-group-item"><?php echo "نام:" . $row['name'] . " امتیاز:" . $row['score']; ?></li>
<?php
what should i do tu solve my data base connection?
You try to connect to a db host without credentials?
If No:
I would say, that your external db host doesn't allow external access.
Did you connect from your local system to ghadir1.shatelhost.com?
If yes:
I think you have to specify the user credentials
Looking at the error string, I can say there is some issue with provided username and password. Mysql is not allowing you too connect to database with given credentials. Please check whether database credentials are right.
$DB_USER = '';
$DB_PASS = '';
You cannot login to database without username. You've to provide username and password to connect the mysql database.
I'm trying to connect my PHP webpage with my MS SQL database. I've got this code from the internet, but have tried others. All seems to come back with a problem with "mssql_connect".
I have tried (I think) everything and can not find out why it won't work.
My code is:
<?php
$myServer = 'SQL5008.Smarterasp.net,1433';
$myUser = '*****';
$myPass = '*****';
$myDB = '*****';
//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 ";
$query .= "FROM tblEmployees ";
$query .= "WHERE CompanyID=3";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
Why dont you try using PDO. I use it with SQL SERVER daily. You will need the php sqlsrv extenstion though.
// instantiate the pdo object
try {
$Server = "localhost";
$User = "username";
$Pass = "password";
$Database = "mydb";
$this->conn = new PDO("sqlsrv:Server=$Server;Database=$Database", $User, $Pass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//allow for SQL query errors
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Check using phpinfo() if mssql extension is loaded. According to PHP Manual:
This extension is not available anymore on Windows with PHP 5.3 or later.
If you find out mssql is unloaded, try to connect using sqlsrv extension. Here you can find a few examples http://php.net/manual/ru/function.sqlsrv-connect.php
Anyway it's a good idea to post here an error message you get from PHP.
I am trying to connect to my local Database from the webserver but i get
Fatal error: Call to undefined function odbc_connect()
in -/-/-/7001238/web/s/sage2.php on line 15"
Any help on how to fix issue.
Here is the code i used to connect.
$odbc['dsn'] = "Sage50";
$odbc['user'] = "Peach";
$odbc['pass'] = "XXXX";
$mysql['host'] = "localhost";
$mysql['user'] = "root";
$mysql['pass'] = "";
$mysql['dbname'] = "sagetest";
$mysql['idfield'] = "id";
$debug=true;
// Step 1: Connect to the source ODBC and target mysql database
if ($debug) echo "Connect to " . $odbc['dsn'] . ' as ' . $odbc['user'] . "\n";
$conn = odbc_connect($odbc['dsn'], $odbc['user'], $odbc['pass']);
if (!$conn) {
die("Error connecting to the ODBC database: " . odbc_errormsg());
}
$myconn = mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);
if (!$myconn)
die("Error connecting to the MySQL database: " . $mysql_error());
if (!mysql_select_db($mysql['dbname'], $myconn)) die("Error selecting the database: " . mysql_error());
// Step 1.5: loop through each table with steps 2-7
$allTables = odbc_tables($conn);
$tablesArray = array();
while (odbc_fetch_row($allTables)) {
if (odbc_result($allTables, "TABLE_TYPE") == "TABLE") {
$tablesArray[] = odbc_result($allTables, "TABLE_NAME");
}
}
Thank you for your time!
First: This error happens because you don't have the ODBC PHP extension installed.
Check http://php.net/manual/en/odbc.installation.php too.
In debian distros you can solve this with a apt-get install php5-odbc, but you can check this also with your hosting provider.
When you see a Call to undefined function you always must check the php.net to be sure about the name of function, or the extension is not loaded.
PS 1: I think you're trying to compare/transfer data between two databases, right?
PS 2: Make sure your server can reach the ODBC address. The webserver is not your dev machine, so localhost is not the real localhost ;)
I did an express install for MS SQL Server 2008 and created a database with a test table. I am running Windows Server 2008 and have IIS, PHP and MS SQL installed. I chose to go with SQL server authentication, where you would need credentials to modify or view the database. I try to print out a simple test table on a web page, but it keeps failing and only returns a white screen..
<?php
$server = "WS1\SQLEXPRESS";
$username = "sa";
$password = "password";
$db = "testdb1";
$dbhandle = mssql_connect($server, $username, $password)
or die ("Cannot connect to SQL Server on $server");
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
echo "You are connected to the " . $db . "database on the " . $server . ".";
$query = "SELECT * FROM table1";
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
echo "<li>" . $row[""] . $row[""] . "</li>";
while ($row = mssql_fetch_array($result)) {
print_r($row);
}
mssql_close($dbhandle);
?>
Ah, the horrific white screen. Sometimes very hard to troubleshoot. Often times it's a simple missing semicolon ...
like the one here:
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
Put a semicolon on the end of that statement and you are back in business.