I need to connect to a remote mysql server using a php page;the connection itself works as a charm, but the moment i try to create the database, as it doesn't exist yet, i get this error:
exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user '[myusername]'#'%' to database '[mydbname]'' in [myurl]/php/db_manager.php:76
As you can see, i have access denied to "%".
Now: what is "%"?
Furthermore:
Main file
private function createDB() {
if($this->cm->update("CREATE DATABASE IF NOT EXISTS " . $this->dbName . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;", array())) { // Error here
$this->cm->update("USE " . $this->dbName . ";", array());
return true;
}
return false;
}
$this->cm is an instance of a correctly initialized PDO wrapper
PDO wrapper file
public function update($query, $values)
try{
$sql = $this->db->prepare($query);
$sql->execute($values);
return true;
} catch(PDOException $e) {
$this->l->error($e); // <- Error here
return false;
}
}
$this->db is a correctly instantiated, fully connected PDO object;These are the lines used to connect
$this->db = new PDO($connection, $this->db_user, $this->db_password, array(PDO::ATTR_PERSISTENT => true));
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
I have full access on the Mysql server
Access denied for user '[myusername]'#'%' to database '[mydbname]'
MySQL permissions are granular: not all users have full access to all databases on the server.
You need to sign in with an administrator and grant the appropriate permissions. For instance, to grant full access:
GRANT ALL
ON mydbname.*
TO 'myusername'#'%'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
... or you can be more selective to your liking:
GRANT SELECT, ALTER, CREATE, DELETE, DROP, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE
ON mydbname.*
TO 'myusername'#'%';
FLUSH PRIVILEGES;
Please check Privileges Supported by MySQL for a full list.
% is a wildcard explained in detail at Account Names and Passwords that means "connection from any host".
I had similar problem on xampp. changed my password to auto generated password(by Generate Password Button) and then copy + paste in config file. worked successfully!
code :
// config information
require_once "config.php";
// make connection string
$connection_string = "mysql:host=" . DB_HOST . ":" . DB_PORT . ";dbname=" . DB_NAME;
$connection = null;
// show connection string
echo $connection_string."</br>";
// try to connect to db or catch exceptions
try{
$connection = new PDO( $connection_string, DB_USER, DB_PASS );
}catch (PDOException $exc){
echo '<pre>';
print_r($exc);
echo '</pre>';
}
echo '</br>';
// connection status
if ( $connection ) {
echo "Connected Successfully!";
} else {
echo "Connection Failed!";
}
Result:
mysql:host=127.0.0.1:3306;dbname=php_pdo_db
Connected Successfully!
In the PDO query add the port after host, (in my case changed the port to 8111) and it should work.
code :
//write in database
try {
$dbh = new PDO("mysql:host=$db_host:8111;dbname=$db_name", $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec($database);
}
catch(PDOException $e) {
if ($e->getCode() == 2002) {
header('location: step3.php?_error=Unable to Connect Database, Please make sure Host info is correct and try again !');
exit;
}
elseif ($e->getCode() == 1045) {
header('location: step3.php?_error=Unable to Connect Database, Please make sure database username and password is correct and try again !');
exit;
}
elseif ($e->getCode() == 1049) {
header('location: step3.php?_error=Unable to Connect Database, Please make sure database name is correct and try again !');
exit;
}
}
Related
$servername= "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=testing",$username, $password);
//set the PDO error mode exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "connected successfully";
} catch(PDOException $e) {
echo " connection failed: " . $e->getMessage();
}
Please check if:
user with username username really exists. With default Mysql instalation the standard username is root, not username.
the user with username has the password password. In default MYsql installation the user root has no password set.
Check via commandline whether you can connect to your database:
https://dev.mysql.com/doc/refman/8.0/en/connecting.html
mysql --host=localhost --user=myname --password=password mydb
I believe you are not knowing your credentials to access the database... How did you install the MySQL database?
I have a simple database on my localhost and I am following a book
I created tables in it but after I used shared file addresses i.e include ['SERVER_DOCUMENT'].'path of file' the access to PHPMyAdmin is denied.
IT says that:
Cannot connect: invalid settings.
mysqli_real_connect(): (HY000/1130): Host 'localhost' is not allowed to connect to this MariaDB server
Connection for controluser as defined in your configuration failed.
mysqli_real_connect(): (HY000/1130): Host 'localhost' is not allowed to connect to this MariaDB server
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.
I tried uninstalling XAMPP but when I reached the same point again on my book and did everything from start it still did the same thing
<?php
try {
$pdo = new PDO('mysql:hostname=localhost;dbname=ijdb', 'ijdbuser', 'mypassword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e) {
$error = 'Unable to connect to server' . $e->getMessage();
include 'error.html';
exit();
}
?>
index.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
if (isset($_GET['addjoke'])) {
include 'form.html';
exit();
}
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
if (isset($_POST['joketext'])) {
try {
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();
} catch (PDOException $e) {
$error = 'Error adding submitted joke: ' . $e->getMessage();
include 'error.html';
exit();
}
header('Location: .');
exit();
}
if (isset($_GET['deletejoke'])) {
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try {
$sql = 'DELETE FROM joke WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e) {
$error = 'Error deleting joke: ' . $e->getMessage();
include 'error.html';
exit();
}
header('Location: .');
exit();
}
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try {
$sql = 'SELECT joke.id,name,email,joketext FROM joke INNER JOIN author ON authorid=author.id';
$result = $pdo->query($sql);
} catch (PDOException $e) {
$error = 'Error fetching jokes: ' . $e->getMessage();
include 'error.html';
exit();
}
//while ($row = $result->fetch())
foreach ($result as $value) {
$jokes[] = array(
'id' => $value['id'], 'text' => $value['joketext'], 'email' => $value['email'], 'name' => $value['name']
);
}
include 'jokes.html';
?>
I expect my joke page to be loaded.
Here is an image showing the errors I listed above:
This is a case where a careful reading of the error message will help.
The phpMyadmin message is probably the most useful one. Let's parse it.
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection.
OK, the localhost server is running.
You should check the host, username and password in your configuration
For some reason, the username / password combination you gave it is wrong.
and make sure that they correspond to the information given by the administrator of the MySQL server.
YOU are the administrator of that MySQL server. Read the xampp docs and look up the username and password to use. In a new xampp installation, the default MySQL username is (or once was) root. The default password is a zero-length string. If you wish to use a different username/password combination from your php program, you must
add that new username/password combination to your MySQL server, logging in from phpmyadmin with the old username to do that. Here's a suggestion about that.
update your db.inc.php file or whatever in your php application gives the username/password.
Your php program logs in to MySQL. MySQL is server software that happens to be running on your local machine when you use xampp. So both it and MySQL must agree on a username/password combination.
With respect, I think you should reread the book's section on how things are set up.
I tried with "mysqld --skip-grant-tables" in xampp shell but that was not a permanent solution because whenever I closed the shell command window the problem came back. Then I found an excellent permanent solution.
You can see the entire procedure is described here
https://www.youtube.com/watch?v=vzs9Z12OTE4
Hope this will fix your issue permanently.
I searched through internet annd found this command 'mysqld --skip-grant-tables'
Executing this in the xampp shell probably resets the priviliges not sure though but got my phpmyadmin page back.
I am learning MySQL/PHP and I cannot figure out how to connect to MySQL on my localhost. I have written a short bit of code and I included root as my user name and root as my password because I have not set these elements yet (as far as I know). I feel that perhaps I am missing something in regards to the username/password combination. However, I feel that this should not be an issue because I have not tampered with the default conditions.
I need some help.
My code is below:
<?php
try {
$db = new PDO("mysql:host=localhost;dbname=shirts4max;port=3306", "root", "root");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf-8'");
} catch (Exception $e) {
echo "Could not connect to the database.";var_dump($e);
exit;
}
I am only seeing the error message on my page:
"Could not connect to the database."
Thank for reading. Please help me Obiwan.
Show the real error. Avoid using root except for maintenance.
From the PDO Manual page here, modified for you
<?php
$dsn = 'mysql:dbname=shirts4max;host=localhost';
$user = 'root';
$password = 'root';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Have you tried connecting via command line using: mysql -uroot -proot -hlocalhost -p3306? If you can't connect that way, the PDO connection won't go through either. Playing with the connection info via command line gives you a nice easy way to find what works, then include it in your code. Also, if you've not set the user/pw, it's possible there isn't one currently, so you wouldn't need those parameters at all.
I'm trying to connect to my mySQL database using the PDO class in PHP.
Here is my Code :
// Connects to Our Database via PDO.
if($local) {
try {
$db = new PDO("mysql:=localhost;dbname=bbc_archive;port=3306", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the DataBase was not possible. ";
die();
}
} else {
try {
$db = new PDO("mysql:=bbcarchive.db.11505263.hostedresource.com;dbname=bbcarchive", "bbcarchive", "myPassword");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the live DataBase was not possible. ";
die();
}
}
The $local variable is defined before and determines whether or not the script is running on a the live server or a test server.
When running in my local environment everthing works fine but on my live server it echo's out "Connection to the live DataBase was not possible." from the catch block.
I've contacted my host provider (godaddy) and they think it's a coding error. I've also, obviously, checked the hostname, dbname, username and password a 100 times and it's all correct. I just can't see the problem!
How can i do this ?
Your DSN seems to be incorrect. The documentation on MySQL DSNs indicates that it should look somewhat like this:
$db = new PDO("mysql:host=localhost;dbname=bbc_archive;port=3306", "root", "");
Hi guys i want to connect USERNAME,PASSWORD protected MS ACCESS 2000 (.*mdb) database using PHP, ODBC or any other database connection. But given below code show WARNING and doesn't connect to the database. please anyone help.
CODE 1
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$user = 'test';
$password ='123';
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename;",$user,$password);
if($connection)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
WARNING MESSAGE
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Microsoft Access Driver] You do not have the necessary permissions to use the '(unknown)' object. Have your system administrator or the person who created this object establish the appropriate permissions for you., SQL state 42000 in SQLConnect in C:\xampp\htdocs\punch\test.php on line 6
Output -> Connection Failed
CODE 2
I tried given below code also. there are no any ERRORS or WARNING MESSAGE but not connect to the access database.
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$systemDbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\SYSTEM.MDW";
$user = 'test';
$password ='123';
$adoCon = new COM("ADODB.Connection") or die("Cannot start ADO");
$connection= $adoCon->Open("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename; SystemDB=$systemDbFilename;Uid=$user; Pwd=$password;");
if(($connection->State) > 0)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
Output -> Connection Failed
CORRECT ANSWER
Finally I found the answer. I tried PDO to connect ODBC.
WORKING CODE
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\Door\DB\IMSDB.mdb;SystemDB=D:\Door\DB\SYSTEM.MDW;Uid=test;Pwd=123;");
if($dbh)
{
echo "Connection Success";
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}