-2

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:

image

Zain Aftab
  • 703
  • 7
  • 21
Bilal Malik
  • 23
  • 1
  • 9
  • those error message tell me that your error is with your phpMyAdmin configuration -- not the code you have shown; check your phpMyAdmin configuration (in the way that the error message states) – landru27 Jun 27 '19 at 11:50
  • Have you tried this? https://stackoverflow.com/questions/11513888/phpmyadmin-wont-stop-throwing-1130-host-localhost-is-not-allowed-to-conne – paradise Jun 27 '19 at 12:04
  • @landru27 it doesnt even open the phpmyadmin link – Bilal Malik Jun 27 '19 at 12:08
  • @BilalMalik : correct; your browser won't open phpMyAdmin until phpMyAdmin is able to connect to the database; for that phpMyAdmin needs to be configured with the appropriate username and password; that configuration has to be done without using the browser – landru27 Jun 27 '19 at 12:23
  • @landru27 through where? – Bilal Malik Jun 27 '19 at 12:25
  • @landru27 I set the user and password from my db.inc.php file and didn't modify it either so where do i need to configure my database? – Bilal Malik Jun 27 '19 at 12:26
  • @BilalMalik : phpMyAdmin isn't using that db.inc.php file; that file is being used by your code; I recommend that you google (or similar) for "setting up phpMyAdmin" -- possibly the book you are following is skipping over some important details – landru27 Jun 27 '19 at 12:30
  • @BilalMalik : also, as an aside, by including `db.inc.php` multiple times, you are forming a brand new database connection each time; that's not optimal; the usual thing to do is to connect to the database once, perform one or more queries, and disconnect at the end; possibly the book you are following isn't very clear on that point, either; possibly it's not a very good book to learn from – landru27 Jun 27 '19 at 12:42
  • To qualify and clarify landru's answer. Each of your PHP scripts should only load the MySQL connection details **once**. The connection is terminated naturally at the end of the script. – Martin Jun 27 '19 at 15:26
  • @Martin i understand that but i have checked all butt mysql is just not running. – Bilal Malik Jun 27 '19 at 16:12
  • @landru27 the problem is fixed by this command 'mysqld --skip-grant-tables' thanks for your help.. – Bilal Malik Jun 28 '19 at 10:56

3 Answers3

2

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.

user1052478
  • 129
  • 1
  • 3
1

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.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
0

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.

Bilal Malik
  • 23
  • 1
  • 9