Welcome Guest, Not a member yet? Register   Sign In
CI4 + Lando (docker) - Main connection [MySQLi]: No such file or directory
#3

(07-16-2021, 08:28 AM)paulbalandan Wrote: Try  changing the host name to either 'localhost' or '127.0.0.1'
No solution using 127.0.0.1
Lando makes his own hostname, which defaults to 'database'


This does work:
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{

 public function 
index()
 {
 
$this->testDatabase('database''lamp''lamp''lamp');
 }

 private function 
testDatabase(?string $hostname null, ?string $username null, ?string $password null, ?string $database null)
 {
 
$conn mysqli_connect($hostname$username$password$database);

 
// Check connection
 
if ($conn->connect_error) {
 
var_dump("Connection failed: " $conn->connect_error);
 die();
 }
 
var_dump("Connected successfully");

 
// Check insert
 
$sql "INSERT INTO news (news_title, news_slug, news_description) VALUES ('Title', 'title', 'Lorem ipsum')";
 if (
mysqli_query($conn$sql)) {
 
$last_id mysqli_insert_id($conn);
 
var_dump("New record created successfully. Last inserted ID is: " $last_id);
 } else {
 
var_dump(mysqli_error($conn));
 die();
 }
 }


But this does not;
This is the error message: Unable to connect to the database. Main connection [MySQLi]: Connection refused
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{

 public function 
index()
 {
 
$db = \Config\Database::connect();
 if (
$db->connect_error) {
 
var_dump("Connection failed: " $db->connect_error);
 die();
 }
 echo 
'connect success';

 
$news $db->query('SELECT news_id, news_slug, news_title FROM news')->getResult();
 
var_dump($news);

 } 

Reply


Messages In This Thread
RE: CI4 + Lando (docker) - Main connection [MySQLi]: No such file or directory - by CCAldo - 07-19-2021, 02:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB