Access denied to localhost/phpmyadmin (HY000/1130) privileges error - php

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.

Related

PDO access denied for user 'username'#'%'

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;
}
}

Can't Link To MySQL database

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.

Connect PDO with Oracle database

I am new to Oracle, installed the Oracle today the 11g Express Edition.
Then I installed Java SDK, and then the free Oracle SQL Developer.
I connected with system account and created a username and table as defined below. I don't exactly know how Oracle works, I think instead of database name, usernames are used. So below are details.
Username/Connection/Database = CustomSearch
Table = Reservation_General_2
There are some columns inside that table and some data. but the point is I cant connect to Oracle Server.
Here is how I tried to connect to database server.
<?php
/**
* Created by PhpStorm.
* User: HaiderHassan
* Date: 9/3/14
* Time: 9:52 PM
*/
header('Access-Control-Allow-Origin: *');
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
";
try {
$conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
Problem is when I try to open that page, I get this error.
ERROR: could not find driver
These are my connection settings when I connect from Oracle Sql Developer.
What am I doing wrong, what steps should I take to fix this issue?
Update
I added the driver by removing semicolon from the php.ini file
extension=php_pdo_oci.dll
But I started getting this error.
The program can't start because OCI.dll is missing from your computer. Try reinstalling the program to fix this problem.
I have to click 4 time OK for different alert boxes that shows up. I also downloaded oci.dll and copied it to the windows/system32, but still getting this error. What to do?
Update
I uninstalled XAMPP and followed this guide to install Apache and PHP separately,
http://www.oracle.com/technetwork/articles/dsl/technote-php-instant-12c-2088811.html
and then I tried my luck. That driver Problem went away but there is new problem
ERROR: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12521: TNS:listener does not currently know of instance requested in connect descriptor (ext\pdo_oci\oci_driver.c:635)
Here below is my new connection String.
try {
$conn = new PDO('oci:dbname=//localhost:1521/xe/ORCL', 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
I tried to follow this answer on Stack Overflow for making a connection string.
http://stackoverflow.com/questions/11970261/connect-oracle-with-pdo-with-sid-and-instance-name
Update 2
Also tried to check if drivers installed. I used this code
foreach(PDO::getAvailableDrivers() as $driver)
echo $driver, '\n';
Got this code from this below link
http://stackoverflow.com/questions/23239433/could-not-connect-to-oracle-using-pdo
it echoes this below line
oci\n
So this means that it is installed or this means some drivers are missing?
Update 3
Again rolled back to old connection just changed some stuff in that connection and seems like connection to oracle worked.
try {
$conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to database';
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
with this I get the message 'Connected to database', means echo works because there is no error given by PDO.
But problem is now my query is not working? What happened to my query? Or will I have to change the syntax of the query also as I connected to Oracle? Or is the connection still not working?
Check PDO and OCI drivers installed properly or not
Try with following code
class PDOConnection {
private $dbh;
function __construct() {
try {
$server = "127.0.0.1";
$db_username = "SYSTEM";
$db_password = "Oracle_1";
$service_name = "ORCL";
$sid = "ORCL";
$port = 1521;
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";
//$this->dbh = new PDO("mysql:host=".$server.";dbname=".dbname, $db_username, $db_password);
$this->dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function select($sql) {
$sql_stmt = $this->dbh->prepare($sql);
$sql_stmt->execute();
$result = $sql_stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function insert($sql) {
$sql_stmt = $this->dbh->prepare($sql);
try {
$result = $sql_stmt->execute();
} catch (PDOException $e) {
trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
}
if ($result) {
return $sql_stmt->rowCount();
}
}
function __destruct() {
$this->dbh = NULL;
}
}
$dbh = new PDOConnection();
$dbh->select($select_sql);
$dbh->insert($insert_sql);
Have you installed the PDO driver? Look at the output of phpinfo() to see what's installed and/or enabled in your environment.
PDO Installation
If you're running PHP on linux, you can see what PDO drivers are available for your distribution by running yum list php-pdo. You can install the driver by running yum install php-pdo. You may also need to install a database specific driver for your database. Running a yum list php* will show you all the PHP extensions available for installation.
Database Specific Drivers
You need to install instant client on Windows, I used it and it work, see this video, the only that change is in the video when he execute install, you don't have to because in the new zip, doesn't have the execution file. I only have a problem when I make a SELECT query but the connection works just fine.
https://www.youtube.com/watch?v=cZDDI9HFBIU
Contact me if you have any question
I think your problem is with oracle listener configuration, your driver is ok, the error "listener does not currently know of inst.." means there is oracle configuration issue. You must ensure that the parameters in the listener file is exactly the same as in the connection string.
Also your connection string oci:dbname=//localhost:1521/xe/ORCL is incorrect, it should be oci:dbname=//localhost:1521/orcl (host:port/service_name) as indicated in listener.ora file. Ensure the correctness of your connection string using SQL developer.
you may check the below link, this link illustrates the matching of listener.ora and connection string parameters and there is php pdo code snippet at the end with the correct usage of the connection string.
https://www.youtube.com/watch?v=pMQXVihgrrE
https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/
Wrong, Wrong & Wrong.
PHPinfo() will NOT enable the PDO driver nor will it show up.
You do NOT need to download a PDO driver separately the one packaged with your PHP installation will work fine.
You do NOT need to install the instant client as your PHP for windows will have the instant client built-in.
Solution:
Updating IIS7 with PHP manager or updating the PHP ini file within your installation to Enable the DLL.
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_sqlsrv.dll
[PHP_PDO_OCI]
extension=php_pdo_oci.dll

Can't connect to database through PHP PDO class

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", "");

php pdo ¿What am I doing wrong?

I'm learning php pdo; my environment is : NetBeans 6.5.1, XAMPP 1.7.0 and I have this code, which it seems to connect.
If I change dbname to a non existent one, it raises exception "db not exists"
If I change user, it raises "login incorrect")
but when I call $cn->query, it raises:
An unhandled Win32 exception occurred in apache.exe
What am I doing wrong?
$hostname = 'localhost';
$username = 'crb';
$password = 'letmein';
try {
$cn = new PDO("mysql:host=$hostname;dbname=bitacora", $username, $password);
echo 'Connected to database<br />';
$sql = "SELECT * FROM usuario WHERE login = '".$login."' AND clave = '".$clave."'";
// Error here
foreach ($cn->query($sql) as $row) {
print $row['login'] .' - '. $row['clave'] . '<br />';
}
} catch (PDOException $e) {
echo $e->getMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
This is a bug in XAMPP 1.7.0. Upgrade to 1.7.1 or follow these instructions to fix your 1.7.0 installation.
Is $cn valid? Check the return value. What you've described so far doesn't convince me that you're connected.
If you haven't already, I'd make sure your environment was working right.
Check to make sure the user works with MySQL itself (using something like mysqlquery).
Check to make sure php can connect to MySQL. I install phpmyadmin on all new setups (even if I don't leave it in place) to make sure I have a good working connection.
Have PDO through exceptions on errors (see http://us2.php.net/manual/en/pdo.error-handling.php) to see where the errors are occurring right away.
My guess is that you are not ever connecting to MySQL, which would explain the inability to change the database.

Categories