Error: SQLSTATE[HY000] [2002] No such file or directory

后端 未结 2 1657
刺人心
刺人心 2021-01-15 14:01

I know this type of question as probably been asked a couple of times, but they all have something to do with laravel, in my case this is vanilla php with no framework.

相关标签:
2条回答
  • 2021-01-15 14:36

    I finally solved the problem, thanks @Dlk for your help.

    The cause of the problem was because in database.php, I was referring to the host for mysql as localhost instead of the name of the MYSQL service in the docker-compose.yml file. Hence database.php file should look like so :

    <?php
    
    $host = 'nairobi_mysql'; // Must be the service name of the database in `docker-compose.yml`
    $db_name = 'nairobi';
    $user = 'admin';
    $pass = '123456';
    $charset = 'utf8mb4'; // Always set charset for database
    $port = '3306'; 
    
    $dsn = "mysql:host=$host;dbname=$db_name;port=$port;charset=$charset";
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    try {
         $pdo = new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
         throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }
    
    ?>
    

    which correlates with my docker-compose.yml file

    version: '3.7'
    
    services:
      php:
        container_name: nairobi_php
        build:
          context: ./
        volumes:
          - './src:/var/www/html'
        depends_on:
          - mysql
        ports:
          - 80:80
    
      mysql:
        container_name: nairobi_mysql
        image: mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: CUeHpADRmZCtnTFGctxp
          MYSQL_DATABASE: nairobi
          MYSQL_USER: admin
          MYSQL_PASSWORD: 123456
        restart: always
        command: --default-authentication-plugin=mysql_native_password
        ports:
          - 3306:3306
    
      adminer:
        image: adminer
        restart: always
        ports:
          - 8080:8080
    
    
    0 讨论(0)
  • 2021-01-15 14:42

    First of all change your connection like this: replace codes in Database.php with the following codes and read comments in codes.

    $host = 'localhost';
    $db   = 'nairobi';
    $user = 'admin';
    $pass = '123456';
    $charset = 'utf8mb4'; // Always set charset for database
    $port = '3308'; //Your port can be 3306 or 3307
    
    $dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    try {
         $dbh = new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
         throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }
    

    Now your codes! you are using prepare statements which makes your codes safe enough. Check if url to Database.php file is correct and change yours to this

    require_once "./library/Database.php";
        if (isset($_POST['submit'])) {
    
          $first_name = $_POST['first_name'];
          $last_name = $_POST['last_name'];
          $email = $_POST['email'];
          $reference_code = $_POST['reference_code'];
        //You have a `TABLE` name call members so you dont need `TABLE` its causing problem
        //Your query should look like this
          $sql = 'INSERT INTO members (first_name, last_name, email, reference_code) VALUES (:first_name, :last_name, :email, :reference_code)';
          $stmt = $dbh->prepare($sql);
          $stmt->execute([$first_name, $last_name, $email, $reference_code]);
        //I removed array here which you dont need, you can directly add fields in execute.
          $stmt->closeCursor();
        //Use `closeCursor()` to free your connection instead unset or close.
        }
    

    Final make sure you created a database. make sure you created a table call members in database.

    if you have done that all, your codes will work without any problem.

    HTML form should look like this, you need to add multi encrypt into form if you want to upload images.

    <form action="yourpage.php" method="post">
      <div class="container">first_name</b></label>
        <input type="text" placeholder="Enter first_name" name="first_name" required>
    
        <label for="last_name"><b>last_name</b></label>
        <input type="text" placeholder="Enter last_name" name="last_name" required>
    
        <label for="email"><b>email</b></label>
        <input type="email" placeholder="Enter email" name="email" required>
    
        <label for="reference_code"><b>reference_code</b></label>
        <input type="text" placeholder="Enter reference_code" name="reference_code" required>
    
        <button type="submit">Login</button>
      </div>
    </form>
    

    Datatable should look like this :

    CREATE TABLE IF NOT EXISTS `members` (
      `member_id` int(8) NOT NULL AUTO_INCREMENT,
      `first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `last_name` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
      `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `reference_code` int(11) NOT NULL,
      PRIMARY KEY (`member_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    0 讨论(0)
提交回复
热议问题