I connected SQL server using PHP v5.2. (using mssql_connect() function()).
And then, I tried to select database.
But I meet error. Error message like this.
[MSSQL error : The server principal "NT AUTHORITY\IUSR" is not able to access the database "***" under the current security context.]
How to solve this?
My code like this...
$this->link_id = mssql_connect($this->server, $this->user, $this->password) or die($html->render());
mssql_select_db($this->dbname, $this->link_id) or die ('Could not ' . $this->dbname);
Related
Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');
I just started programming in php and would like to ask a question about the database selection code for mysql in the php coding.
I used phpmyadmin to create a database "admin" when in phpmyadmin I click on privileges and see the name as"admin#127.0.0.1". I created a connection to the database using this code in PHP:
<?php $connection = mysqli_connect("127.0.0.1", "admin", "admin123");
if (!$connection)
die("Database connection failed:" . mysqli_error());
and now i want to select the tables in the database I use this command:
$selected = mysqli_select_db("admin", $connection);
if (!$selected)
{
die('Database selection failed:' .mysqli_error());
}
?>
I know it connects because when only using the connection command when opening my broswer I can see the header i put in html, but I get an error with the selection command and cannot continue.
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\Program Files\EasyPHP-DevServer-13.1VC11\data\localweb\projects\databaZE.php on line 6##
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\Program Files\EasyPHP-DevServer-13.1VC11\data\localweb\projects\databaZE.php on line 8
Database selection failed:
Firstly is there a problem with the way I wrote my database name thats why it cannot connect and giving me error msg's?I used 127.0.0.1 as database, admin#127.0.0.1 but still same msg. I tried both mysql and mysqli but it doesnt seem to work also.
Edit: first time user sorry am a little confused with inputting code.
You have them the wrong way around,
$selected = mysqli_select_db("admin", $connection);
should be
$selected = mysqli_select_db($connection, "admin");
And
die('Database selection failed:' .mysqli_error());
should be
die('Database selection failed:' .mysqli_error($connection));
I'm trying to connect sql server with php, i'm trying to get info from the database..
Now, here is what i got:
try {
$user = '';
$pass = '';
$objDb = new PDO('mysql:host=192.168.10.250;dbname=WEB_POROSIA',
'$user', '$pass');
$objDb->exec('SET CHARACTER SET utf8');
$sql = "SELECT *
FROM 'WEB_POROSIA'
";
$statement = $objDb->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
}
I receive this error:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
This is my first attempt to connect php with sql server, and i don't know what this output means, i mean i don't know what might cause it!
I'm using xampp.
Thanks
It appears that you are trying to connect using the wrong driver.
For Sql Server you might want to use PDO ODBC driver not the mysql method you are trying to use.
You need to use a different DBO driver.
Check out other ODBC drivers, i.e PDO.
The code you have written tries to connect to a MySQL database rather than a MSSQL one.
You'll want to do something like this:
$db_connection = new PDO("dblib:dbname=$db_name;host=$host", $username, $password);
You can find more information here: http://grover.open2space.com/content/use-php-and-pdo-connect-ms-sql-server
I can connect to mysql, but I get an error saying Could not select db.
Is there anything I am missing? Any help is much appreciated.
<?php
$link = mysql_connect('localhost','','');
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}
echo 'Connection OK';
$selected=mysql_select_db("test1",$link) or die("<br>Could q not select db");
mysql_close($link);
?>
Replace
"<br>Could q not select db"
with
"<br>Could q not select db" . mysql_error();
to show detailed mysql error.
Mostly you entered the wrong mysql details. Check if the database exist.
There could be multiple factors preventing you from selecting your MySQL database. The following could be the cause:
Your connecting to the wrong database server. In this case you are using localhost.
Your connecting using the wrong username. In this case you are using no username
Your connecting using the wrong password. In this case you are using no password.
Your trying to select a database that really does not exist. In this case your trying to select test1. If the database does not exist you will need to create it.
You can duplicate what your program is doing by logging onto your console and issuing the following command:
mysql -e "show databases"
This will list all of the databases available to that user.
I am facing database connection problem while trying to connect a .php file through wamp server
the error message is something like- "Access denied for the user " # ' localhost' " for database 'aschool'. 'aschool' is my database name.
Mentioning that I've changed my port number of wamp server, I am worried that is it really
for changing port number or anything else.Here is my code.
$con = mysql_connect();
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("aschool", $con);
After this line the error message comes. I've tried parameters "localhost" inside the mysql_connect()
function or more parameters but the result is same.
Thanks in advance anyone gives me any solution
That's because you are using mysql_connectwrong for your use case.
If you check the documentation page it says that you can also a server-path,
something like mysql_connect('localhost:1234', 'username', 'password').
But you shouldn't use mysql_connect.
Use PDO so that you can use parameterized queries.
In code it would go like this:
try
{
$pdo = new \PDO('mysql:dbname=aschool;host=127.0.0.1', 'myUser', 'myPassword');
} catch (PDOException $exception)
{
// Do something with your exception.
// Echo it, dump it, log it, die it.
// Just don't ignore the exceptions!
}